MimeMessageWriteToAsync(FormatOptions, Stream, CancellationToken) Method

Asynchronously write the message to the specified output stream.

Definition

Namespace: MimeKit
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public Task WriteToAsync(
	FormatOptions options,
	Stream stream,
	CancellationToken cancellationToken = default
)

Parameters

options  FormatOptions
The formatting options.
stream  Stream
The output stream.
cancellationToken  CancellationToken  (Optional)
The cancellation token.

Return Value

Task
An awaitable task.

Implements

IMimeMessageWriteToAsync(FormatOptions, Stream, CancellationToken)

Remarks

Asynchronously writes the message to the output stream using the provided formatting options.

Example

C#
                    public static async Task<byte[]> GetMessageBytesAsync (MimeMessage message, CancellationToken cancellationToken = default)
{
    using (var memory = new MemoryStream ()) {
        // When sending a message via SMTP, the line endings must be CRLF. FormatOptions.Default
        // uses the platform's native line endings, so create options that use DOS line endings.
        var options = FormatOptions.Default.Clone ();
        options.NewLineFormat = NewLineFormat.Dos;

        await message.WriteToAsync (options, memory, cancellationToken).ConfigureAwait (false);

        return memory.ToArray ();
    }
}
                  

Exceptions

ArgumentNullException

options is .

-or-

stream is .

OperationCanceledException The operation was canceled via the cancellation token.
IOException An I/O error occurred.

See Also