System.Net.Mail.MailMessage Class

Represents an e-mail message that can be sent using the System.Net.Mail.SmtpClient class.

See Also: MailMessage Members


public class MailMessage : IDisposable


Instances of the System.Net.Mail.MailMessage class are used to construct e-mail messages that are transmitted to an SMTP server for delivery using the System.Net.Mail.SmtpClient class.

The sender, recipient, subject, and body of an e-mail message may be specified as parameters when a erload:System.Net.Mail.MailMessage.#ctor is used to initialize a System.Net.Mail.MailMessage object. These parameters may also be set or accessed using properties on the System.Net.Mail.MailMessage object.

The primary mail message headers and elements for the message may be set using the following properties of the System.Net.Mail.MailMessage class.



Blind carbon copies (BCC)


Carbon copies (CC)




Encoding for custom headers


Message body












The System.Net.Mail.MailMessage class also allows an application to access the headers collection for the message using the MailMessage.Headers property. While this collection is read-only (a new collection can not be set), custom headers can be added to or deleted from this collection. Any custom headers added will be included when the System.Net.Mail.MailMessage instance is sent. Before a message is sent, only headers specifically added to this collection in the MailMessage.Headers property are included in the collection. After a the System.Net.Mail.MailMessage instance is sent, the MailMessage.Headers property will also include headers that are set using the associated properties of the System.Net.Mail.MailMessage class or parameters passed when a erload:System.Net.Mail.MailMessage.#ctor is used to initialize a System.Net.Mail.MailMessage object.

If some mail headers are malformed, they could cause the email message to become corrupted. So any mail header in the headers collection that can be set using a property on the System.Net.Mail.MailMessage class should only be set using the System.Net.Mail.MailMessage class property or as a parameter passed when a erload:System.Net.Mail.MailMessage.#ctor initializes a System.Net.Mail.MailMessage object. The following list of mail headers should not be added using the MailMessage.Headers property and any values set for these headers using the MailMessage.Headers property will be discarded or overwritten when the message is sent:

If the application does not specify an X-Sender header using the MailMessage.Headers property, the System.Net.Mail.MailMessage class will create one when the message is sent.

Use the MailMessage.AlternateViews property to specify copies of an e-mail message in different formats. For example, if you send a message in HTML, you might also want to provide a plain text version in case some of the recipients use e-mail readers that cannot display HTML content. For an example that demonstrates creating a message with alternate views, see MailMessage.AlternateViews.

Use the MailMessage.Attachments property to add attachments to an e-mail message. For an example that demonstrates creating a message with an attachment, see MailMessage.Attachments.

After assembling your e-mail message, you can send it by using the SmtpClient.Send(MailMessage) or erload:System.Net.Mail.SmtpClient.SendAsync methods.


Namespace: System.Net.Mail
Assembly: System (in System.dll)
Assembly Versions:,
Since: .NET 2.0