Class MailMessage

MailMessage class

Represents an e-mail message: its headers, body, attachments and recipients. Load it from or save it to EML, MSG, MHTML, HTML and other formats, and send it with the mail clients.

public class MailMessage : IDisposable, IEnumerable<MailMessage>, IMessage, 
    IPreferredTextEncodingProvider

Constructors

NameDescription
MailMessage()Initializes a new instance of the MailMessage class: an empty message.
MailMessage(MailAddress, MailAddress)Initializes a new instance of the MailMessage class with a sender and a recipient.
MailMessage(string, string)Initializes a new instance of the MailMessage class with a sender and recipients.
MailMessage(string, string, string, string)Initializes a new instance of the MailMessage class with a sender, recipients, a subject and a plain-text body.

Properties

NameDescription
virtual AlternateViews { get; }Gets the alternate views of the body: the plain-text, HTML and other versions.
virtual Attachments { get; }Gets the attachments of the message.
virtual Bcc { get; set; }Gets or sets the BCC recipients.
virtual Body { get; set; }Gets or sets the plain-text body. When the message has no plain-text part, the text of HtmlBody without the HTML markup is returned.
virtual BodyEncoding { get; set; }Gets or sets the character encoding of the body.
BodyType { get; }Gets the format of the body: plain text, HTML or RTF.
virtual CC { get; set; }Gets or sets the CC recipients.
virtual Date { get; set; }Gets or sets the date and time of the message (the Date header).
virtual DeliveryNotificationOptions { get; set; }Gets or sets when the SMTP server sends a delivery status notification to the sender.
Epilogue { get; set; }Gets or sets the text after the last MIME boundary of the message.
virtual From { get; set; }Gets or sets the author of the message (the From header).
virtual Headers { get; }Gets the headers of the message.
virtual HtmlBody { get; set; }Gets or sets the HTML body.
virtual IsBodyHtml { get; set; }Gets or sets a value indicating whether the body is HTML.
virtual IsDraft { get; set; }Gets or sets a value indicating whether the message is a draft that has not been sent (the X-Unsent header).
virtual IsEncrypted { get; }Gets a value indicating whether the message is encrypted (S/MIME).
virtual IsReadOnly { get; }Gets a value indicating whether the message is read-only, as a signed copy is.
virtual IsSigned { get; }Gets a value indicating whether the message is digitally signed.
virtual ItemId { get; }Gets the identifiers of the message in the mailbox it was retrieved from.
virtual LinkedResources { get; }Gets the resources embedded in the HTML body, such as images.
virtual MessageId { get; set; }Gets or sets the unique identifier of the message (the Message-ID header).
virtual OriginalIsTnef { get; }Gets a value indicating whether the message was loaded from TNEF data (winmail.dat).
Preamble { get; set; }Gets or sets the text before the first MIME boundary, usually a note for mail clients that do not support MIME.
PreferredTextEncoding { get; set; }Gets or sets the encoding used for all text of the message when no other encoding is specified.
virtual Priority { get; set; }Gets or sets the priority of the message: normal, low or high.
ReadReceiptTo { get; set; }Gets or sets the addresses a read receipt is requested to (the Disposition-Notification-To header).
virtual ReplyToList { get; set; }Gets or sets the addresses replies should go to (the Reply-To header).
virtual ReversePath { get; set; }Gets or sets the address bounces are returned to (the Return-Path header). When the message is sent, it is used as the envelope sender in preference to Sender and From.
virtual Sender { get; set; }Gets or sets the agent that sent the message on behalf of the author (the Sender header).
virtual Sensitivity { get; set; }Gets or sets the sensitivity of the message: personal, private or company confidential.
virtual Subject { get; set; }Gets or sets the subject of the message.
virtual SubjectEncoding { get; set; }Gets or sets the character encoding of the subject.
TimeZoneOffset { get; set; }Gets or sets the offset from UTC that the dates of the message are written with.
virtual To { get; set; }Gets or sets the To recipients.
virtual XMailer { get; set; }Gets or sets the software that created the message (the X-Mailer header).

Methods

NameDescription
static Load(Stream)Loads a message from a stream. MSG is recognized automatically; any other data is read as EML.
static Load(string)Loads a message from a file. MSG is recognized automatically; any other file is read as EML.
static Load(Stream, LoadOptions)Loads a message from a stream in the format selected by the options.
static Load(string, LoadOptions)Loads a message from a file in the format selected by the options.
virtual AddAlternateView(AlternateView)Adds an alternate view of the body to the message.
virtual AddAttachment(Attachment)Adds an attachment to the message.
virtual AttachSignature(CmsSigner)Creates a signed copy of the message (S/MIME). The copy is read-only.
virtual AttachSignature(X509Certificate2)Creates a signed copy of the message (S/MIME). The copy is read-only.
virtual AttachSignature(CmsSigner, bool)Creates a signed copy of the message (S/MIME). The copy is read-only.
virtual AttachSignature(X509Certificate2, bool)Creates a signed copy of the message (S/MIME). The copy is read-only.
virtual CheckBounced()Checks whether the message is a bounce (a non-delivery report) and, if so, why it bounced.
virtual CheckSignature()Verifies the signature of the message.
virtual Clone()Creates a copy of the message.
CreateReadReceipt(string, string)Creates a read receipt for the message.
virtual Decrypt()Decrypts the message with a certificate found in the certificate store.
virtual Decrypt(X509Certificate2)Decrypts the message with the specified certificate.
Dispose()Releases the resources used by the message, including its attachments.
virtual Encrypt(X509Certificate2)Creates a copy of the message encrypted for one recipient (S/MIME).
virtual Encrypt(X509Certificate2[])Creates a copy of the message encrypted for several recipients (S/MIME).
override Equals(object)Determines whether the specified object is equal to this message.
GetAlternateViewContent(string)Gets the content of the alternate view of the specified media type.
GetEnumerator()Returns an enumerator that iterates through a collection.
override GetHashCode()Returns the hash code of the message.
virtual GetHtmlBodyText(bool)Gets the text of the HTML body without the markup.
virtual GetHtmlBodyText(HyperlinkRenderingCallback)Gets the text of the HTML body without the markup, rendering hyperlinks with a custom method.
virtual Import(Stream)Replaces the content of this message with a message read from a stream in EML format.
RecomposeTnefContent()Rebuilds the TNEF attachment (winmail.dat) of a message that was loaded from TNEF, so that it reflects the changes made to the message.
virtual RemoveSignature()Returns a copy of a signed message without the signature.
virtual Save(Stream)Saves the message to a stream in EML format.
virtual Save(string)Saves the message to a file in EML format, whatever the file extension.
virtual Save(Stream, SaveOptions)Saves the message to a stream in the format selected by the options.
virtual Save(string, SaveOptions)Saves the message to a file in the format selected by the options.
virtual SetHtmlBody(string, bool)Sets the HTML body, optionally detecting its encoding.
override ToString()Returns a string that represents the message.
static CheckSignature(Stream)Verifies the signature of a message read from a stream in EML format.
static CheckSignature(string)Verifies the signature of a message in an EML file.

Remarks

Load a message with Load, save it with Save. Without options, Load recognizes MSG and otherwise reads EML, and Save writes EML; pass a LoadOptions or SaveOptions object for other formats. To work with Outlook-specific properties, convert the message to a MapiMessage.

Examples

The following example shows how to create a new Email Message and save with SaveOptions.

[C#]

var message = new MailMessage
{
    // Set subject of the message, Html body and sender information
    Subject = "New message created by Aspose.Email for .NET",
    HtmlBody = "<b>This line is in bold.</b> <br/> <br/>" +
               "<font color=blue>This line is in blue color</font>",
    From = new MailAddress("from@domain.com", "Sender Name", false)
};

// Add TO recipients and Add CC recipients
message.To.Add(new MailAddress("to1@domain.com", "Recipient 1", false));
message.To.Add(new MailAddress("to2@domain.com", "Recipient 2", false));
message.CC.Add(new MailAddress("cc1@domain.com", "Recipient 3", false));
message.CC.Add(new MailAddress("cc2@domain.com", "Recipient 4", false));

// Save message to EML, EMLX, MSG and MHTML formats
message.Save("CreateNewMailMessage_out.eml", SaveOptions.DefaultEml);
message.Save("CreateNewMailMessage_out.emlx", SaveOptions.CreateSaveOptions(MailMessageSaveType.EmlxFormat));
message.Save("CreateNewMailMessage_out.msg", SaveOptions.DefaultMsgUnicode);
message.Save("CreateNewMailMessage_out.mhtml", SaveOptions.DefaultMhtml);

[Visual Basic]

' Create a new instance of MailMessage class
Dim message As MailMessage = New MailMessage()

' Set subject of the message, Html body and sender information
message.Subject = "New message created by Aspose.Email for .NET"
message.HtmlBody = "<b>This line is in bold.</b> <br/> <br/>" & "<font color=blue>This line is in blue color</font>"
message.From = New MailAddress("from@domain.com", "Sender Name", False)

' Add TO recipients and Add CC recipients
message.[To].Add(New MailAddress("to1@domain.com", "Recipient 1", False))
message.[To].Add(New MailAddress("to2@domain.com", "Recipient 2", False))
message.CC.Add(New MailAddress("cc1@domain.com", "Recipient 3", False))
message.CC.Add(New MailAddress("cc2@domain.com", "Recipient 4", False))

' Save message in EML, EMLX, MSG and MHTML formats
message.Save("CreateNewMailMessage_out.eml", SaveOptions.DefaultEml)
message.Save("CreateNewMailMessage_out.emlx", SaveOptions.CreateSaveOptions(MailMessageSaveType.EmlxFormat))
message.Save("CreateNewMailMessage_out.msg", SaveOptions.DefaultMsgUnicode)
message.Save("CreateNewMailMessage_out.mhtml", SaveOptions.DefaultMhtml)

See Also