Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
MailMessage Class
 Headers Property
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
MailMessage..::.Headers Property

Updated: November 2007

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

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

Visual Basic (Declaration)
Public ReadOnly Property Headers As NameValueCollection
Visual Basic (Usage)
Dim instance As MailMessage
Dim value As NameValueCollection

value = instance.Headers
C#
public NameValueCollection Headers { get; }
Visual C++
public:
property NameValueCollection^ Headers {
    NameValueCollection^ get ();
}
J#
/** @property */
public NameValueCollection get_Headers()
JScript
public function get Headers () : NameValueCollection

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.

C#
        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();
        }

Visual C++
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 = L"\\\\share3\\c$\\reports\\data.xls";

   // Create a message and set up the recipients.
   MailMessage^ message = gcnew MailMessage( L"ReportMailer@contoso.com",to,L"Quarterly data report",L"See the attached spreadsheet." );

   // Create  the file attachment for this e-mail message.
   Attachment^ data = gcnew 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 = gcnew SmtpClient( server );

   // Add credentials if the SMTP server requires them.
   client->Credentials = dynamic_cast<ICredentialsByHost^>(CredentialCache::DefaultNetworkCredentials);
   client->Send( message );

   // Display the message headers.
   array<String^>^keys = message->Headers->AllKeys;
   Console::WriteLine( L"Headers" );
   IEnumerator^ myEnum3 = keys->GetEnumerator();
   while ( myEnum3->MoveNext() )
   {
      String^ s = safe_cast<String^>(myEnum3->Current);
      Console::WriteLine( L"{0}:", s );
      Console::WriteLine( L"    {0}", message->Headers[ s ] );
   }

   data->~Attachment();
   client->~SmtpClient();
}



J#
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.get_ContentDisposition();
    disposition.set_CreationDate(System.IO.File.GetCreationTime(file));
    disposition.set_ModificationDate(System.IO.File.GetLastWriteTime(file));
    disposition.set_ReadDate(System.IO.File.GetLastAccessTime(file));
    disposition.set_DispositionType(DispositionTypeNames.Attachment);
    // Add the file attachment to this e-mail message.
    message.get_Attachments().Add(data);
    //Send the message.
    SmtpClient client = new SmtpClient(server);
    // Add credentials if the SMTP server requires them.
    client.set_Credentials((ICredentialsByHost)(
        CredentialCache.get_DefaultNetworkCredentials()));
    client.Send(message);
    // Display the message headers.
    String keys[] = message.get_Headers().get_AllKeys();
    Console.WriteLine("Headers");
    for (int iCtr = 0; iCtr < keys.length; iCtr++) {
        String s = keys[iCtr];
        Console.WriteLine("{0}:", s);
        Console.WriteLine("    {0}", message.get_Headers().get_Item(s));
    }
    data.Dispose();
} //CreateMessageWithAttachment4

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, 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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker