Click or drag to resize
MimeKit

IMimeContentDecodeTo Method

Decode the content stream into another stream.

Namespace:  MimeKit
Assembly:  MimeKit (in MimeKit.dll) Version: 3.0.0
Syntax
C#
void DecodeTo(
	Stream stream,
	CancellationToken cancellationToken = default
)

Parameters

stream
Type: System.IOStream
The output stream.
cancellationToken (Optional)
Type: System.ThreadingCancellationToken
The cancellation token.
Exceptions
ExceptionCondition
ArgumentNullExceptionstream is null.
OperationCanceledException The operation was cancelled via the cancellation token.
IOException An I/O error occurred.
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.
Examples
C#
foreach (var attachment in message.Attachments) {
    if (attachment is MessagePart) {
        var fileName = attachment.ContentDisposition?.FileName;
        var rfc822 = (MessagePart) attachment;

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

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

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