MailMessage Class

Definition

Represents an email message that can be sent using the SmtpClient class.

C#
public class MailMessage : IDisposable
Inheritance
MailMessage
Implements

Examples

The following code example demonstrates creating and sending an email message that includes an attachment.

C#
public static void CreateMessageWithAttachment(string server)
{
    // Specify the file to be attached and sent.
    // This example assumes that a file named Data.xls exists in the
    // current working directory.
    string file = "data.xls";
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
        "jane@contoso.com",
        "ben@contoso.com",
        "Quarterly data report.",
        "See the attached spreadsheet.");

    // Create  the file attachment for this email message.
    Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
    // Add time stamp information for the file.
    ContentDisposition disposition = data.ContentDisposition;
    disposition.CreationDate = System.IO.File.GetCreationTime(file);
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
    disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
    // Add the file attachment to this email message.
    message.Attachments.Add(data);

    //Send the message.
    SmtpClient client = new SmtpClient(server);
    // Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
            ex.ToString());
    }
    // Display the values in the ContentDisposition for the attachment.
    ContentDisposition cd = data.ContentDisposition;
    Console.WriteLine("Content disposition");
    Console.WriteLine(cd.ToString());
    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);
    foreach (DictionaryEntry d in cd.Parameters)
    {
        Console.WriteLine("{0} = {1}", d.Key, d.Value);
    }
    data.Dispose();
}

Remarks

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

The sender, recipient, subject, and body of an email 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 Property
Attachments Attachments
Blind carbon copies (BCC) Bcc
Carbon copies (CC) CC
Content-Type BodyEncoding
Encoding for custom headers HeadersEncoding
Message body Body
Priority Priority
Recipient To
Reply-To ReplyToList
Sender From
Subject Subject

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 email 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 email 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 email message. For an example that demonstrates creating a message with an attachment, see Attachments. Calling Dispose on the MailMessage also calls Dispose on each referenced Attachment.

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

Constructors

MailMessage()

Initializes an empty instance of the MailMessage class.

MailMessage(MailAddress, MailAddress)

Initializes a new instance of the MailMessage class by using the specified MailAddress class objects.

MailMessage(String, String, String, String)

Initializes a new instance of the MailMessage class.

MailMessage(String, String)

Initializes a new instance of the MailMessage class by using the specified String class objects.

Properties

AlternateViews

Gets the attachment collection used to store alternate forms of the message body.

Attachments

Gets the attachment collection used to store data attached to this email message.

Bcc

Gets the address collection that contains the blind carbon copy (BCC) recipients for this email message.

Body

Gets or sets the message body.

BodyEncoding

Gets or sets the encoding used to encode the message body.

BodyTransferEncoding

Gets or sets the transfer encoding used to encode the message body.

CC

Gets the address collection that contains the carbon copy (CC) recipients for this email message.

DeliveryNotificationOptions

Gets or sets the delivery notifications for this email message.

From

Gets or sets the from address for this email message.

Headers

Gets the email headers that are transmitted with this email message.

HeadersEncoding

Gets or sets the encoding used for the user-defined custom headers for this email message.

IsBodyHtml

Gets or sets a value indicating whether the mail message body is in HTML.

Priority

Gets or sets the priority of this email message.

ReplyTo
Obsolete.
Obsolete.
Obsolete.

Gets or sets the ReplyTo address for the mail message.

ReplyToList

Gets the list of addresses to reply to for the mail message.

Sender

Gets or sets the sender's address for this email message.

Subject

Gets or sets the subject line for this email message.

SubjectEncoding

Gets or sets the encoding used for the subject content for this email message.

To

Gets the address collection that contains the recipients of this email message.

Methods

Dispose()

Releases all resources used by the MailMessage.

Dispose(Boolean)

Releases the unmanaged resources used by the MailMessage and optionally releases the managed resources.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1