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 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(
			   "ReportMailer@contoso.com",
			   to,
			   "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.
			message.Attachments.Add(data);
			//Send the message.
			SmtpClient client = new SmtpClient(server);
			// Add credentials if the SMTP server requires them.
			client.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials;
			client.Send(message);
			// Display the message headers.
			string[] keys = message.Headers.AllKeys;
			Console.WriteLine("Headers");
			foreach (string s in keys)
			{
				Console.WriteLine("{0}:", s);
				Console.WriteLine("    {0}", message.Headers[s]);
			}
			data.Dispose();
		}
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 = S"\\\\share3\\c$\\reports\\data.xls";
         // Create a message and set up the recipients.
         MailMessage* message = new MailMessage(
            S"ReportMailer@contoso.com", 
            to,
            S"Quarterly data report", 
            S"See the attached spreadsheet.");

        // Create  the file attachment for this e-mail message.
        Attachment* data = new Attachment();
        // Use the name parameter to set the name of the file to 
        // Qtr3.xls.
        data->SetContentFromFile(file, S"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.
        message->Attachments->Add(data);
        //Send the message.
        SmtpClient* client = new SmtpClient(server);
        // Add credentials if the SMTP server requires them.
        client->Credentials = dynamic_cast<ICredentialsByHost*> (CredentialCache::DefaultNetworkCredentials);
        client->Send(message);
        // Display the message headers.
        String* keys[] = message->Headers->AllKeys;
        Console::WriteLine(S"Headers");
       IEnumerator* myEnum3 = keys->GetEnumerator();
       while (myEnum3->MoveNext())
       {
           String* s = __try_cast<String*>(myEnum3->Current);
            Console::WriteLine(S"{0}:",s);
            Console::WriteLine(S"    {0}", message->Headers->Item[s]);
       }
       data->Dispose();
       client->Dispose();
    }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: