MimeReaderReadMessage Method

Read a message from the stream.

Definition

Namespace: MimeKit
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public void ReadMessage(
	CancellationToken cancellationToken = default
)

Parameters

cancellationToken  CancellationToken  (Optional)
The cancellation token.

Remarks

Reads a message from the stream.

Example

C#
                    // MimeReader is a low-level, forward-only reader that reports the structure of a MIME
// stream via protected virtual callbacks rather than constructing a MimeMessage. This
// makes it ideal for scanning large messages without loading their content into memory.
class MimeStructureReader : MimeReader
{
    readonly TextWriter output;
    int depth;

    public MimeStructureReader (Stream stream, TextWriter output) : base (stream, MimeFormat.Entity)
    {
        this.output = output;
    }

    void WriteLine (string format, params object[] args)
    {
        output.Write (new string (' ', depth * 2));
        output.WriteLine (format, args);
    }

    protected override void OnHeaderRead (Header header, int beginLineNumber, CancellationToken cancellationToken)
    {
        if (header.Id == HeaderId.Subject)
            WriteLine ("Subject: {0}", header.Value);
    }

    protected override void OnMultipartBegin (ContentType contentType, long beginOffset, int beginLineNumber, CancellationToken cancellationToken)
    {
        WriteLine ("{0} (line {1})", contentType.MimeType, beginLineNumber);
        depth++;
    }

    protected override void OnMultipartEnd (ContentType contentType, long beginOffset, int beginLineNumber, long headersEndOffset, long endOffset, int lines, CancellationToken cancellationToken)
    {
        depth--;
    }

    protected override void OnMessagePartBegin (ContentType contentType, long beginOffset, int beginLineNumber, CancellationToken cancellationToken)
    {
        WriteLine ("{0} (line {1})", contentType.MimeType, beginLineNumber);
        depth++;
    }

    protected override void OnMessagePartEnd (ContentType contentType, long beginOffset, int beginLineNumber, long headersEndOffset, long endOffset, int lines, CancellationToken cancellationToken)
    {
        depth--;
    }

    protected override void OnMimePartEnd (ContentType contentType, long beginOffset, int beginLineNumber, long headersEndOffset, long endOffset, int lines, CancellationToken cancellationToken)
    {
        // The offsets make it possible to locate (and later extract) the raw part
        // within the original stream.
        WriteLine ("{0} (line {1}, {2} bytes)", contentType.MimeType, beginLineNumber, endOffset - beginOffset);
    }
}

public static void PrintMimeStructure (string fileName)
{
    using (var stream = File.OpenRead (fileName)) {
        var reader = new MimeStructureReader (stream, Console.Out);

        // ReadMessage() parses the stream, invoking the callbacks along the way.
        reader.ReadMessage ();
    }
}
                  

Exceptions

OperationCanceledException The operation was canceled via the cancellation token.
FormatException There was an error parsing the message.
IOException An I/O error occurred.

See Also