MimeMessage Class

A message in the Internet Message Format ([RFC 5322]) with its MIME structure ([RFC 2045]): the content of an .eml file.

public sealed class MimeMessage

Example

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;
}

Constructors

NameDescription
MimeMessage()Initializes a new instance of the MimeMessage class with an empty text body.

MimeMessage()

MimeMessage.cs

Initializes a new instance of the MimeMessage class with an empty text body.

public MimeMessage()

Properties

NameDescription
AttachmentsGets the attachments: the leaf parts and the embedded messages, except the text, HTML and calendar bodies.
BccGets or sets the blind carbon copy recipients.
BodyGets or sets the body: a text part, a multipart, or any other entity.
CalendarBodyGets the calendar body: the first text/calendar part that is not an attachment, such as the invitation of a meeting (iMIP, RFC 6047).
CcGets or sets the carbon copy recipients.
DateGets or sets the date of the message, or null when it has none or it is invalid.
FromGets or sets the authors of the message.
HeadersGets the headers of the message, except the Content-* headers, which are the headers of MimeMessage.Body.
HtmlBodyGets the HTML body: the first text/html part that is not an attachment.
InReplyToGets or sets the identifier of the message this one replies to, without its angle brackets.
MessageIdGets or sets the identifier of the message, without its angle brackets.
ReferencesGets or sets the identifiers of the messages of the thread, without their angle brackets.
ReplyToGets or sets the addresses replies go to.
SenderGets or sets the sender, when it is not the author.
SubjectGets or sets the subject.
TextBodyGets the plain text body: the first text/plain part that is not an attachment.
ToGets or sets the primary recipients.

Attachments

MimeMessage.cs

Gets the attachments: the leaf parts and the embedded messages, except the text, HTML and calendar bodies.

public IEnumerable<MimeEntity> Attachments { get; }

Gets or sets the blind carbon copy recipients.

public IReadOnlyList<MailboxAddress> Bcc { get; set; }

Gets or sets the body: a text part, a multipart, or any other entity.

public MimeEntity Body { get; set; }

Property value

MimeEntity

CalendarBody

MimeMessage.cs

Gets the calendar body: the first text/calendar part that is not an attachment, such as the invitation of a meeting (iMIP, RFC 6047).

public string? CalendarBody { get; }

Property value

string?

Gets or sets the carbon copy recipients.

public IReadOnlyList<MailboxAddress> Cc { get; set; }

Gets or sets the date of the message, or null when it has none or it is invalid.

public DateTimeOffset? Date { get; set; }

Property value

DateTimeOffset?

Gets or sets the authors of the message.

public IReadOnlyList<MailboxAddress> From { get; set; }

Gets the headers of the message, except the Content-* headers, which are the headers of MimeMessage.Body.

public MimeHeaderCollection Headers { get; }

Property value

MimeHeaderCollection

Gets the HTML body: the first text/html part that is not an attachment.

public string? HtmlBody { get; }

Property value

string?

Gets or sets the identifier of the message this one replies to, without its angle brackets.

public string? InReplyTo { get; set; }

Property value

string?

Gets or sets the identifier of the message, without its angle brackets.

public string? MessageId { get; set; }

Property value

string?

References

MimeMessage.cs

Gets or sets the identifiers of the messages of the thread, without their angle brackets.

public IReadOnlyList<string> References { get; set; }

Property value

IReadOnlyList<string>

Gets or sets the addresses replies go to.

public IReadOnlyList<MailboxAddress> ReplyTo { get; set; }

Gets or sets the sender, when it is not the author.

public MailboxAddress? Sender { get; set; }

Property value

MailboxAddress?

Gets or sets the subject.

public string? Subject { get; set; }

Property value

string?

Gets the plain text body: the first text/plain part that is not an attachment.

public string? TextBody { get; }

Property value

string?

Gets or sets the primary recipients.

public IReadOnlyList<MailboxAddress> To { get; set; }

Methods

NameDescription
Load(Stream, MimeReadOptions?)Reads a message from a stream, until its end.
Load(ReadOnlyMemory<byte>, MimeReadOptions?)Reads a message.
Load(string, MimeReadOptions?)Reads a message from a file, such as an .eml file.
ToByteArray()Returns the bytes of the message.
WriteTo(Stream)Writes the message. The parts read from a message and not modified are written as they were read.
WriteTo(string)Writes the message to a file.

Load overloads

Load(Stream, MimeReadOptions?)

MimeMessage.cs

Reads a message from a stream, until its end.

public static MimeMessage Load(Stream stream, MimeReadOptions? options = null)
Parameters
stream Stream
The stream.
options MimeReadOptions?
The options, or null for the default options.
Returns

MimeMessage The message.

Exceptions
EmailResourceLimitException
The message is larger than MimeReadOptions.MaxSize.

Load(ReadOnlyMemory<byte>, MimeReadOptions?)

MimeMessage.cs

Reads a message.

public static MimeMessage Load(ReadOnlyMemory<byte> data, MimeReadOptions? options = null)
Parameters
data ReadOnlyMemory<byte>
The content of the message.
options MimeReadOptions?
The options, or null for the default options.
Returns

MimeMessage The message.

Exceptions
EmailResourceLimitException
The message exceeds a limit of options.

Load(string, MimeReadOptions?)

MimeMessage.cs

Reads a message from a file, such as an .eml file.

public static MimeMessage Load(string path, MimeReadOptions? options = null)
Parameters
path string
The path of the file.
options MimeReadOptions?
The options, or null for the default options.
Returns

MimeMessage The message.

ToByteArray()

MimeMessage.cs

Returns the bytes of the message.

public byte[] ToByteArray()

Returns

byte[] The bytes of the message.

WriteTo overloads

WriteTo(Stream)

MimeMessage.cs

Writes the message. The parts read from a message and not modified are written as they were read.

public void WriteTo(Stream stream)
Parameters
stream Stream
The stream.

WriteTo(string)

MimeMessage.cs

Writes the message to a file.

public void WriteTo(string path)
Parameters
path string
The path of the file.