This documentation is archived and is not being maintained.

MailMessage Class

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


Namespace:  System.Net.Mail
Assembly:  System (in System.dll)

Public Class MailMessage _
	Implements IDisposable

The MailMessage type exposes the following members.

Public methodMailMessageInitializes an empty instance of the MailMessage class.
Public methodMailMessage(MailAddress, MailAddress)Initializes a new instance of the MailMessage class by using the specified MailAddress class objects.
Public methodMailMessage(String, String)Initializes a new instance of the MailMessage class by using the specified String class objects.
Public methodMailMessage(String, String, String, String)Initializes a new instance of the MailMessage class.

Public propertyAlternateViewsGets the attachment collection used to store alternate forms of the message body.
Public propertyAttachmentsGets the attachment collection used to store data attached to this e-mail message.
Public propertyBccGets the address collection that contains the blind carbon copy (BCC) recipients for this e-mail message.
Public propertyBodyGets or sets the message body.
Public propertyBodyEncodingGets or sets the encoding used to encode the message body.
Public propertyCCGets the address collection that contains the carbon copy (CC) recipients for this e-mail message.
Public propertyDeliveryNotificationOptionsGets or sets the delivery notifications for this e-mail message.
Public propertyFromGets or sets the from address for this e-mail message.
Public propertyHeadersGets the e-mail headers that are transmitted with this e-mail message.
Public propertyHeadersEncodingGets or sets the encoding used for the user-defined custom headers for this e-mail message.
Public propertyIsBodyHtmlGets or sets a value indicating whether the mail message body is in Html.
Public propertyPriorityGets or sets the priority of this e-mail message.
Public propertyReplyTo Obsolete. Gets or sets the ReplyTo address for the mail message.
Public propertyReplyToListGets or sets the list of addresses to reply to for the mail message.
Public propertySenderGets or sets the sender's address for this e-mail message.
Public propertySubjectGets or sets the subject line for this e-mail message.
Public propertySubjectEncodingGets or sets the encoding used for the subject content for this e-mail message.
Public propertyToGets the address collection that contains the recipients of this e-mail message.

Public methodDisposeReleases all resources used by the MailMessage.
Protected methodDispose(Boolean)Releases the unmanaged resources used by the MailMessage and optionally releases the managed resources.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

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

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

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

Mail header or part




Blind carbon copies (BCC)


Carbon copies (CC)




Encoding for custom headers


Message body












The MailMessage class also allows an application to access the headers collection for the message using the 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 MailMessage instance is sent. Before a message is sent, only headers specifically added to this collection in the Headers property are included in the collection. After a the MailMessage instance is sent, the Headers property will also include headers that are set using the associated properties of the MailMessage class or parameters passed when a MailMessage is used to initialize a 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 MailMessage class should only be set using the MailMessage class property or as a parameter passed when a MailMessage initializes a MailMessage object. The following list of mail headers should not be added using the Headers property and any values set for these headers using the Headers property will be discarded or overwritten when the message is sent:

  • Bcc

  • Cc

  • Content-ID

  • Content-Location

  • Content-Transfer-Encoding

  • Content-Type

  • Date

  • From

  • Importance

  • MIME-Version

  • Priority

  • Reply-To

  • Sender

  • To

  • X-Priority

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

Use the 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 AlternateViews.

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

After assembling your e-mail message, you can send it by using the Send or SendAsync methods.

The following code example demonstrates creating and sending an e-mail message that includes an attachment.

Public Shared Sub CreateMessageWithAttachment(ByVal server As String)
    ' Specify the file to be attached and sent.
    ' This example assumes that a file named Data.xls exists in the
    ' current working directory.
    Dim file As String = "data.xls"
    ' Create a message and set up the recipients.
    Dim message As New MailMessage("", "", "Quarterly data report.", "See the attached spreadsheet.")

    ' Create  the file attachment for this e-mail message.
    Dim data As New Attachment(file, MediaTypeNames.Application.Octet)
    ' Add time stamp information for the file.
    Dim disposition As ContentDisposition = data.ContentDisposition
    disposition.CreationDate = IO.File.GetCreationTime(file)
    disposition.ModificationDate = IO.File.GetLastWriteTime(file)
    disposition.ReadDate = IO.File.GetLastAccessTime(file)
    ' Add the file attachment to this e-mail message.

    'Send the message.
    Dim client As New SmtpClient(server)
    ' Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials

    Catch ex As Exception
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString())
    End Try
    ' Display the values in the ContentDisposition for the attachment.
    Dim cd As ContentDisposition = data.ContentDisposition
    Console.WriteLine("Content disposition")
    Console.WriteLine("File {0}", cd.FileName)
    Console.WriteLine("Size {0}", cd.Size)
    Console.WriteLine("Creation {0}", cd.CreationDate)
    Console.WriteLine("Modification {0}", cd.ModificationDate)
    Console.WriteLine("Read {0}", cd.ReadDate)
    Console.WriteLine("Inline {0}", cd.Inline)
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count)
    For Each d In cd.Parameters
        Console.WriteLine("{0} = {1}", d.Key, d.Value)
    Next d
End Sub

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.