MimeContentDecodeToAsync Method

Asynchronously decode the content stream into another stream.

Definition

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

Parameters

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

Return Value

Task
An awaitable task.

Implements

IMimeContentDecodeToAsync(Stream, CancellationToken)

Remarks

If the content stream is encoded, this method will decode it into the output stream using a suitable decoder based on the Encoding property, otherwise the stream will be copied into the output stream as-is.

Example

C#
                    foreach (var attachment in message.Attachments) {
    if (attachment is MessagePart) {
        var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
        var rfc822 = (MessagePart) attachment;

        if (string.IsNullOrEmpty (fileName))
            fileName = "attached-message.eml";

        // make sure that the filename value does not contain a full path or invalid path characters
        fileName = Path.GetFileName (fileName);

        using (var stream = File.Create (fileName))
            rfc822.Message.WriteTo (stream);
    } else {
        var part = (MimePart) attachment;
        var fileName = part.FileName;

        if (string.IsNullOrEmpty (fileName))
            fileName = "untitled.dat";

        // make sure that the filename value does not contain a full path or invalid path characters
        fileName = Path.GetFileName (fileName);

        using (var stream = File.Create (fileName))
            part.Content.DecodeTo (stream);
    }
}
                  

Exceptions

ArgumentNullException stream is .
ObjectDisposedException The MimeContent has been disposed.
OperationCanceledException The operation was canceled via the cancellation token.
IOException An I/O error occurred.

See Also