Overview

edit

MimeMessage is a message in the Internet Message Format (RFC 5322) with its MIME structure (RFC 2045 to RFC 2049): the content of an .eml file, of a file of a Maildir folder, or of a message of an mbox file. MimeMessage.Load reads one from a path, a stream or bytes:

var message = MimeMessage.Load(path);
Assert.Equal("Attachments", message.Subject);
Assert.Equal("alice@example.com", message.From[0].Address);
Assert.Equal("See the attachments.", message.TextBody?.TrimEnd());

foreach (var attachment in message.Attachments.OfType<MimePart>())
{
    // The file name is decoded (RFC 2047 and RFC 2231), and the content is decoded from base64 or quoted-printable
    var fileName = attachment.FileName;
    var content = attachment.GetContent();
    Assert.NotNull(content);
    _ = fileName;
}

Headers and addresses

Headers are the headers of the message in their order, and the members of the message read the common ones: From, To, Subject, Date, MessageId... The values are decoded: the encoded words of RFC 2047 (=?utf-8?B?...?=), the headers in UTF-8 (RFC 6532), the dates of RFC 5322 and their obsolete forms, the groups and the comments of the address lists.

Parts

Body is the root of the MIME tree:

  • a MimePart is a leaf: a text or a file. GetContent decodes it from base64, quoted-printable or uuencode, and GetText decodes a text with its charset;
  • a MimeMultipart contains parts (multipart/mixed, alternative, related, digest...);
  • a MimeMessagePart contains a message (message/rfc822), such as a forwarded message.

TextBody and HtmlBody are the first text and HTML parts that are not attachments, and Attachments are the other leaves, including the inline images of an HTML body. The parameters of MimeContentType and MimeContentDisposition, such as the file names, are decoded from RFC 2231 (filename*=utf-8''...) and from the encoded words some clients use.

Malformed messages

The reader does not reject a malformed message: it reads what it can, as email clients do. A line that is not a header ends the headers, a multipart without its close delimiter ends at the end of the message, a multipart without a boundary has its content in its Preamble, and an unknown charset is read as UTF-8, or Latin-1 when the bytes are not UTF-8. MimeReadOptions limits the depth of the tree, the number of parts and the size of a message read from a stream.