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 MimeMessageExample
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
| Name | Description |
|---|---|
| MimeMessage() | Initializes a new instance of the MimeMessage class with an empty text body. |
MimeMessage()
MimeMessage.csInitializes a new instance of the MimeMessage class with an empty text body.
public MimeMessage()Properties
| Name | Description |
|---|---|
| Attachments | Gets the attachments: the leaf parts and the embedded messages, except the text, HTML and calendar bodies. |
| Bcc | Gets or sets the blind carbon copy recipients. |
| Body | Gets or sets the body: a text part, a multipart, or any other entity. |
| CalendarBody | Gets the calendar body: the first text/calendar part that is not an attachment, such as the invitation of a meeting (iMIP, RFC 6047). |
| Cc | Gets or sets the carbon copy recipients. |
| Date | Gets or sets the date of the message, or null when it has none or it is invalid. |
| From | Gets or sets the authors of the message. |
| Headers | Gets the headers of the message, except the Content-* headers, which are the headers of MimeMessage.Body. |
| HtmlBody | Gets the HTML body: the first text/html part that is not an attachment. |
| InReplyTo | Gets or sets the identifier of the message this one replies to, without its angle brackets. |
| MessageId | Gets or sets the identifier of the message, without its angle brackets. |
| References | Gets or sets the identifiers of the messages of the thread, without their angle brackets. |
| ReplyTo | Gets or sets the addresses replies go to. |
| Sender | Gets or sets the sender, when it is not the author. |
| Subject | Gets or sets the subject. |
| TextBody | Gets the plain text body: the first text/plain part that is not an attachment. |
| To | Gets or sets the primary recipients. |
Attachments
MimeMessage.csGets the attachments: the leaf parts and the embedded messages, except the text, HTML and calendar bodies.
public IEnumerable<MimeEntity> Attachments { get; }Property value
Gets or sets the blind carbon copy recipients.
public IReadOnlyList<MailboxAddress> Bcc { get; set; }Property value
Body
MimeMessage.csGets or sets the body: a text part, a multipart, or any other entity.
public MimeEntity Body { get; set; }Property value
CalendarBody
MimeMessage.csGets or sets the carbon copy recipients.
public IReadOnlyList<MailboxAddress> Cc { get; set; }Property value
Date
MimeMessage.csGets or sets the date of the message, or null when it has none or it is invalid.
public DateTimeOffset? Date { get; set; }Property value
From
MimeMessage.csGets or sets the authors of the message.
public IReadOnlyList<MailboxAddress> From { get; set; }Property value
Headers
MimeMessage.csGets the headers of the message, except the Content-* headers, which are the headers of MimeMessage.Body.
public MimeHeaderCollection Headers { get; }Property value
HtmlBody
MimeMessage.csInReplyTo
MimeMessage.csMessageId
MimeMessage.csReferences
MimeMessage.csGets or sets the identifiers of the messages of the thread, without their angle brackets.
public IReadOnlyList<string> References { get; set; }Property value
ReplyTo
MimeMessage.csGets or sets the addresses replies go to.
public IReadOnlyList<MailboxAddress> ReplyTo { get; set; }Property value
Sender
MimeMessage.csGets or sets the sender, when it is not the author.
public MailboxAddress? Sender { get; set; }Property value
Subject
MimeMessage.csTextBody
MimeMessage.csGets or sets the primary recipients.
public IReadOnlyList<MailboxAddress> To { get; set; }Property value
Methods
| Name | Description |
|---|---|
| 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.csReads a message from a stream, until its end.
public static MimeMessage Load(Stream stream, MimeReadOptions? options = null)Parameters
streamStream- The stream.
optionsMimeReadOptions?- The options, or
nullfor the default options.
Returns
MimeMessage The message.
Exceptions
EmailResourceLimitException- The message is larger than
MimeReadOptions.MaxSize.
Load(ReadOnlyMemory<byte>, MimeReadOptions?)
MimeMessage.csReads a message.
public static MimeMessage Load(ReadOnlyMemory<byte> data, MimeReadOptions? options = null)Parameters
dataReadOnlyMemory<byte>- The content of the message.
optionsMimeReadOptions?- The options, or
nullfor the default options.
Returns
MimeMessage The message.
Exceptions
EmailResourceLimitException- The message exceeds a limit of
options.
Load(string, MimeReadOptions?)
MimeMessage.csReads a message from a file, such as an .eml file.
public static MimeMessage Load(string path, MimeReadOptions? options = null)Parameters
pathstring- The path of the file.
optionsMimeReadOptions?- The options, or
nullfor the default options.
Returns
MimeMessage The message.