| IMailFolderGetStream(UniqueId, BodyPart, CancellationToken, ITransferProgress) Method |
Get a body part as a stream.
Namespace: MailKitAssembly: MailKit (in MailKit.dll) Version: 4.7.1
Syntax Stream GetStream(
UniqueId uid,
BodyPart part,
CancellationToken cancellationToken = default,
ITransferProgress progress = null
)
Parameters
- uid UniqueId
- The UID of the message.
- part BodyPart
- The desired body part.
- cancellationToken CancellationToken (Optional)
- The cancellation token.
- progress ITransferProgress (Optional)
- The progress reporting mechanism.
Return Value
StreamThe body part stream.
Remarks
Gets a body part as a stream.
Example public static void CacheBodyParts (string baseDirectory)
{
using (var client = new ImapClient ()) {
client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
client.Authenticate ("username", "password");
client.Inbox.Open (FolderAccess.ReadOnly);
var query = SearchQuery.SubjectContains ("MimeKit").Or (SearchQuery.SubjectContains ("MailKit"));
var uids = client.Inbox.Search (query);
var items = client.Inbox.Fetch (uids, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure);
foreach (var item in items) {
var directory = Path.Combine (baseDirectory, item.UniqueId.ToString ());
Directory.CreateDirectory (directory);
foreach (var bodyPart in item.BodyParts) {
using (var stream = client.Inbox.GetStream (item.UniqueId, bodyPart)) {
var path = Path.Combine (directory, bodyPart.PartSpecifier);
using (var output = File.Create (path))
stream.CopyTo (output);
}
}
}
client.Disconnect (true);
}
}
See Also