MailMessage.Headers Vlastnost

Definice

Získá hlavičky e-mailu, které jsou přenášeny s touto e-mailovou zprávou.

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

Hodnota vlastnosti

A NameValueCollection , který obsahuje záhlaví e-mailu.

Příklady

Následující příklad kódu ukazuje zobrazení hlaviček pro e-mailovou zprávu.

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

Poznámky

Vlastnost Headers umožňuje aplikaci přístup ke kolekci hlaviček zprávy. I když je tato kolekce jen pro čtení (novou kolekci nelze nastavit), je možné do této kolekce přidat vlastní hlavičky nebo je z této kolekce odstranit. Všechny přidané vlastní hlavičky budou zahrnuty při MailMessage odeslání instance. Před odesláním zprávy jsou do kolekce zahrnuty pouze hlavičky přidané do této kolekce ve Headers vlastnosti. MailMessage Po odeslání instance bude vlastnost také obsahovat hlavičky, Headers které jsou nastaveny pomocí přidružených MailMessage vlastností třídy nebo parametrů předaných při MailMessage inicializaci objektuMailMessage.

Pokud jsou některá záhlaví pošty poškozená, můžou způsobit poškození e-mailové zprávy. Takže všechny hlavičky pošty v kolekci hlaviček, které lze nastavit pomocí vlastnosti MailMessage třídy, by měly být nastaveny pouze pomocí MailMessage vlastnosti třídy nebo jako parametr předaný při MailMessage inicializaci objektu MailMessage . Následující seznam záhlaví pošty by neměl být přidán pomocí Headers vlastnosti a všechny hodnoty nastavené pro tyto hlavičky pomocí Headers vlastnosti budou při odeslání zprávy zahozeny nebo přepsány:

  • Skrytá

  • Pole kopie

  • Content-ID

  • Umístění obsahu

  • Kódování přenosu obsahu

  • Typ obsahu

  • Datum

  • Z

  • Důležitost

  • MIME-Version

  • Priorita

  • Reply-To

  • Odesílatel

  • Záměr

  • Priorita X

Pokud aplikace nezadá hlavičku X-Sender pomocí Headers vlastnosti, MailMessage třída ji vytvoří při odeslání zprávy.

Odesílatel, příjemce, předmět a text e-mailové zprávy mohou být zadány jako parametry, pokud MailMessage se používá k inicializaci objektu MailMessage . Tyto parametry mohou být také nastaveny nebo přístupné pomocí vlastností objektu MailMessage .

Hlavičky a prvky primární e-mailové zprávy mohou být nastaveny pomocí následujících vlastností MailMessage třídy.

Záhlaví nebo část pošty Vlastnost
Přílohy Attachments
Kopie s slepým uhlíkem (BCC) Bcc
Kopie uhlíku (CC) CC
Typ obsahu BodyEncoding
Kódování pro vlastní hlavičky HeadersEncoding
Text zprávy Body
Priorita Priority
Příjemce To
Reply-To ReplyToList
Odesílatel From
Předmět Subject

Platí pro