This documentation is archived and is not being maintained.

MailMessage.Headers Property

Gets the e-mail headers that are transmitted with this e-mail message.

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

public NameValueCollection Headers { get; }

Property Value

Type: System.Collections.Specialized.NameValueCollection
A NameValueCollection that contains the e-mail headers.

The Headers property allows an application to access the headers collection for the message. 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 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.

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 following code example demonstrates displaying the headers for a mail message.

		public static void CreateMessageWithAttachment4(string server, string to)
			// Specify the file to be attached and sent.
			// This example uses a file on a UNC share.
			string file = @"\\share3\c$\reports\data.xls";
			// Create a message and set up the recipients.
			MailMessage message = new MailMessage(
			   "Quarterly data report",
			   "See the attached spreadsheet.");

			// Create  the file attachment for this e-mail message.
			Attachment data = new Attachment("qtr3.xls", 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);
			disposition.DispositionType = DispositionTypeNames.Attachment;
			// Add the file attachment to this e-mail message.
			//Send the message.
			SmtpClient client = new SmtpClient(server);
			// Add credentials if the SMTP server requires them.
			client.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials;
			// Display the message headers.
			string[] keys = message.Headers.AllKeys;
			foreach (string s in keys)
				Console.WriteLine("{0}:", s);
				Console.WriteLine("    {0}", message.Headers[s]);

.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.