Click or drag to resize
MimeKit

MailFolderGetStreamAsync(Int32, CancellationToken, ITransferProgress) Method

Asynchronously get a message stream.

Namespace: MailKit
Assembly: MailKit (in MailKit.dll) Version: 4.7.1
Syntax
C#
public virtual Task<Stream> GetStreamAsync(
	int index,
	CancellationToken cancellationToken = default,
	ITransferProgress progress = null
)

Parameters

index  Int32
The index of the message.
cancellationToken  CancellationToken  (Optional)
The cancellation token.
progress  ITransferProgress  (Optional)
The progress reporting mechanism.

Return Value

TaskStream
The message stream.

Implements

IMailFolderGetStreamAsync(Int32, CancellationToken, ITransferProgress)
Exceptions
ExceptionCondition
ArgumentOutOfRangeException

index is out of range.

ObjectDisposedException The IMailStore has been disposed.
ServiceNotConnectedException The IMailStore is not connected.
ServiceNotAuthenticatedException The IMailStore is not authenticated.
FolderNotOpenException The folder is not currently open.
MessageNotFoundException The IMailStore did not return the requested message stream.
OperationCanceledException The operation was canceled via the cancellation token.
IOException An I/O error occurred.
ProtocolException The server's response contained unexpected tokens.
CommandException The command failed.
Remarks
Asynchronously gets a message stream.
Example
C#
public static void DownloadMessages ()
{
    using (var client = new ImapClient ()) {
        client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);

        client.Authenticate ("username", "password");

        client.Inbox.Open (FolderAccess.ReadOnly);

        for (int index = 0; index < client.Inbox.Count; index++) {
            using (var stream = client.Inbox.GetStream (index)) {
                using (var output = File.Create ($"{index}.eml"))
                    stream.CopyTo (output);
            }
        }

        client.Disconnect (true);
    }
}
See Also