This documentation is archived and is not being maintained.
MailMessage::Headers Property
Visual Studio 2008
Gets the e-mail headers that are transmitted with this e-mail message.
Assembly: System (in System.dll)
Property Value
Type: System.Collections.Specialized::NameValueCollectionA NameValueCollection that contains the e-mail headers.
The following code example demonstrates displaying the headers for a mail message.
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(); }
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.
Show: