MailMessage.Headers Właściwość

Definicja

Pobiera nagłówki wiadomości e-mail przesyłane za pomocą tej wiadomości e-mail.

public:
 property System::Collections::Specialized::NameValueCollection ^ Headers { System::Collections::Specialized::NameValueCollection ^ get(); };
public System.Collections.Specialized.NameValueCollection Headers { get; }
member this.Headers : System.Collections.Specialized.NameValueCollection
Public ReadOnly Property Headers As NameValueCollection

Wartość właściwości

Element NameValueCollection zawierający nagłówki wiadomości e-mail.

Przykłady

Poniższy przykład kodu pokazuje wyświetlanie nagłówków wiadomości e-mail.

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 email 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 email 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();
}
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 email 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 email 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 Shared Sub CreateMessageWithAttachment4(ByVal server As String, ByVal [to] As String)
    ' Specify the file to be attached And sent.
    ' This example uses a file on a UNC share.
    Dim file As String = "\\share3\c$\reports\data.xls"
    Dim message As MailMessage = New MailMessage("ReportMailer@contoso.com", [to], "Quarterly data report", "See the attached spreadsheet.")
    ' Create  the file attachment for this email message.
    Dim data As Attachment = New Attachment("qtr3.xls", MediaTypeNames.Application.Octet)
    ' Add time stamp information for the file.
    Dim disposition As ContentDisposition = 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 email message.
    message.Attachments.Add(data)
    'Send the message.
    Dim client As SmtpClient = New SmtpClient(server)
    ' Add credentials if the SMTP server requires them.
    client.Credentials = CType(CredentialCache.DefaultNetworkCredentials, ICredentialsByHost)
    client.Send(message)
    ' Display the message headers.
    Dim keys As String() = message.Headers.AllKeys
    Console.WriteLine("Headers")

    For Each s As String In keys
        Console.WriteLine("{0}:", s)
        Console.WriteLine("    {0}", message.Headers(s))
    Next

    data.Dispose()
End Sub

Uwagi

Właściwość Headers umożliwia aplikacji dostęp do kolekcji nagłówków dla komunikatu. Chociaż ta kolekcja jest tylko do odczytu (nie można ustawić nowej kolekcji), nagłówki niestandardowe można dodawać lub usuwać z tej kolekcji. Wszystkie dodane nagłówki niestandardowe będą uwzględniane po wysłaniu MailMessage wystąpienia. Przed wysłaniem wiadomości tylko nagłówki dodane do tej kolekcji we Headers właściwości są uwzględniane w kolekcji. Po wysłaniu MailMessageHeaders wystąpienia właściwość będzie również zawierać nagłówki ustawione przy użyciu skojarzonych właściwości MailMessage klasy lub parametrów przekazywanych, gdy MailMessage obiekt jest używany do inicjowania MailMessage obiektu.

Jeśli niektóre nagłówki poczty są źle sformułowane, mogą spowodować uszkodzenie wiadomości e-mail. Dlatego każdy nagłówek poczty w kolekcji nagłówków, który można ustawić przy użyciu właściwości klasy MailMessage , powinien być ustawiony tylko przy użyciu MailMessage właściwości klasy lub jako parametr przekazywany podczas MailMessage inicjowania MailMessage obiektu. Poniższa lista nagłówków poczty nie powinna być dodawana przy użyciu Headers właściwości i żadne wartości ustawione dla tych nagłówków przy użyciu właściwości zostaną odrzucone lub zastąpione po wysłaniu Headers wiadomości:

  • Udw

  • DW

  • Identyfikator zawartości

  • Lokalizacja zawartości

  • Kodowanie transferu zawartości

  • Content-Type

  • Date

  • Źródło

  • Ważność

  • MIME-Version

  • Priorytet

  • Reply-To

  • Nadawca

  • Działanie

  • Priorytet X

Jeśli aplikacja nie określi nagłówka X-Sender przy użyciu Headers właściwości, MailMessage klasa utworzy jedną po wysłaniu komunikatu.

Nadawca, adresat, temat i treść wiadomości e-mail mogą być określone jako parametry, gdy MailMessage obiekt jest używany do inicjowania MailMessage obiektu. Te parametry można również ustawić lub uzyskać do nich dostęp przy użyciu właściwości obiektu MailMessage .

Podstawowe nagłówki wiadomości e-mail i elementy wiadomości mogą być ustawione przy użyciu następujących właściwości MailMessage klasy.

Nagłówek lub część poczty Właściwość
Załączniki Attachments
Niewidome kopie węglowe (BCC) Bcc
Kopie węglowe (CC) CC
Content-Type BodyEncoding
Kodowanie nagłówków niestandardowych HeadersEncoding
Treść wiadomości Body
Priorytet Priority
Adresat To
Reply-To ReplyToList
Nadawca From
Temat Subject

Dotyczy