MailMessage 类

定义

表示可以使用 SmtpClient 类发送的电子邮件。

public ref class MailMessage : IDisposable
public class MailMessage : IDisposable
type MailMessage = class
    interface IDisposable
Public Class MailMessage
Implements IDisposable
继承
MailMessage
实现

示例

下面的代码示例演示如何创建和发送电子邮件,其中包含附件。

static void CreateMessageWithAttachment( String^ server )
{
   
   // Specify the file to be attached and sent.
   // This example assumes that a file named Data.xls exists in the
   // current working directory.
   String^ file = L"data.xls";
   
   // Create a message and set up the recipients.
   MailMessage^ message = gcnew MailMessage( L"jane@contoso.com",L"ben@contoso.com",L"Quarterly data report.",L"See the attached spreadsheet." );
   
   // Create  the file attachment for this email message.
   Attachment^ data = gcnew Attachment(file, 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 );
   
   // 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 = CredentialCache::DefaultNetworkCredentials;
   client->Send( message );
   
   // Display the values in the ContentDisposition for the attachment.
   ContentDisposition^ cd = data->ContentDisposition;
   Console::WriteLine( L"Content disposition" );
   Console::WriteLine( cd );
   Console::WriteLine( L"File {0}", cd->FileName );
   Console::WriteLine( L"Size {0}", cd->Size );
   Console::WriteLine( L"Creation {0}", cd->CreationDate );
   Console::WriteLine( L"Modification {0}", cd->ModificationDate );
   Console::WriteLine( L"Read {0}", cd->ReadDate );
   Console::WriteLine( L"Inline {0}", cd->Inline );
   Console::WriteLine( L"Parameters: {0}", cd->Parameters->Count );
   IEnumerator^ myEnum1 = cd->Parameters->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      DictionaryEntry^ d = safe_cast<DictionaryEntry^>(myEnum1->Current);
      Console::WriteLine( L"{0} = {1}", d->Key, d->Value );
   }

   data->~Attachment();
   client->~SmtpClient();
}
public static void CreateMessageWithAttachment(string server)
{
    // Specify the file to be attached and sent.
    // This example assumes that a file named Data.xls exists in the
    // current working directory.
    string file = "data.xls";
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
        "jane@contoso.com",
        "ben@contoso.com",
        "Quarterly data report.",
        "See the attached spreadsheet.");

    // Create  the file attachment for this email message.
    Attachment data = new Attachment(file, 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);
    // 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 = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
            ex.ToString());
    }
    // Display the values in the ContentDisposition for the attachment.
    ContentDisposition cd = data.ContentDisposition;
    Console.WriteLine("Content disposition");
    Console.WriteLine(cd.ToString());
    Console.WriteLine("File {0}", cd.FileName);
    Console.WriteLine("Size {0}", cd.Size);
    Console.WriteLine("Creation {0}", cd.CreationDate);
    Console.WriteLine("Modification {0}", cd.ModificationDate);
    Console.WriteLine("Read {0}", cd.ReadDate);
    Console.WriteLine("Inline {0}", cd.Inline);
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
    foreach (DictionaryEntry d in cd.Parameters)
    {
        Console.WriteLine("{0} = {1}", d.Key, d.Value);
    }
    data.Dispose();
}
Public Shared Sub CreateMessageWithAttachment(ByVal server As String)
    ' Specify the file to be attached And sent.
    ' This example assumes that a file named Data.xls exists in the
    ' current working directory.
    Dim file As String = "data.xls"
    ' Create a message and set up the recipients.
    Dim message As MailMessage = New MailMessage(
        "jane@contoso.com",
        "ben@contoso.com",
        "Quarterly data report.",
        "See the attached spreadsheet.")

    ' Create  the file attachment for this email message.
    Dim data As Attachment = New Attachment(file, 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)
    ' 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 = CredentialCache.DefaultNetworkCredentials

    Try
        client.Send(message)
    Catch ex As Exception
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString())
    End Try

    ' Display the values in the ContentDisposition for the attachment.
    Dim cd As ContentDisposition = data.ContentDisposition
    Console.WriteLine("Content disposition")
    Console.WriteLine(cd.ToString())
    Console.WriteLine("File {0}", cd.FileName)
    Console.WriteLine("Size {0}", cd.Size)
    Console.WriteLine("Creation {0}", cd.CreationDate)
    Console.WriteLine("Modification {0}", cd.ModificationDate)
    Console.WriteLine("Read {0}", cd.ReadDate)
    Console.WriteLine("Inline {0}", cd.Inline)
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count)

    For Each d As DictionaryEntry In cd.Parameters
        Console.WriteLine("{0} = {1}", d.Key, d.Value)
    Next

    data.Dispose()
End Sub

注解

类的 MailMessage 实例用于构造发送到 SMTP 服务器的电子邮件,以便使用该 SmtpClient 类进行传递。

使用对象初始化MailMessage对象时MailMessage,可以将电子邮件的发件人、收件人、主题和正文指定为参数。 还可以使用对象上的 MailMessage 属性设置或访问这些参数。

可以使用类的以下属性 MailMessage 设置邮件的主邮件头和元素。

邮件头或部件 properties
Attachments Attachments
密件抄送 (密件抄送) Bcc
抄送 (CC) CC
Content-Type BodyEncoding
自定义标头的编码 HeadersEncoding
邮件正文 Body
优先级 Priority
Recipient To
答复 ReplyToList
发送方 From
主题 Subject

MailMessage 类还允许应用程序使用 Headers 属性访问消息的标头集合。 虽然此集合是只读的 (新集合无法设置) ,但可以在此集合中添加或删除自定义标头。 发送实例时 MailMessage ,将包含添加的任何自定义标头。 在发送消息之前,集合中只包含专门添加到该集合中的 Headers 标头。 MailMessage发送实例后,Headers该属性还将包含使用在用于初始化MailMessage对象时MailMessage传递的MailMessage类或参数的关联属性设置的标头。

如果某些邮件头格式不正确,可能会导致电子邮件损坏。 因此,标头集合中可以使用类属性设置的任何邮件头,只能使用MailMessage类属性MailMessage或初始化MailMessage对象时MailMessage传递的参数进行设置。 不应使用属性添加以下邮件头列表, Headers 使用属性为这些标头 Headers 设置的任何值将在发送邮件时被丢弃或覆盖:

  • 密件抄送

  • 抄送

  • Content-ID

  • Content-Location

  • Content-Transfer-Encoding

  • Content-Type

  • Date

  • 重要性

  • MIME-Version

  • 优先级

  • 答复

  • 发送方

  • 功能

  • X 优先级

如果应用程序未使用 Headers 属性指定 X-Sender 标头,则发送消息时,类 MailMessage 将创建一个。

使用 AlternateViews 属性以不同格式指定电子邮件的副本。 例如,如果以 HTML 形式发送邮件,你可能还希望提供纯文本版本,以防某些收件人使用无法显示 HTML 内容的电子邮件阅读器。 有关演示如何使用备用视图创建消息的示例,请参阅 AlternateViews

使用 Attachments 此属性向电子邮件添加附件。 有关演示如何创建带有附件的邮件的示例,请参阅 Attachments。 对 MailMessage 调用 Dispose 还会在每个引用的附件上调用 Dispose。

组合电子邮件后,可以使用或SendAsync方法发送它Send

构造函数

MailMessage()

初始化 MailMessage 类的空实例。

MailMessage(MailAddress, MailAddress)

使用指定的 MailAddress 类对象初始化 MailMessage 类的新实例。

MailMessage(String, String)

使用指定的 String 类对象初始化 MailMessage 类的新实例。

MailMessage(String, String, String, String)

初始化 MailMessage 类的新实例。

属性

AlternateViews

获取用于存储邮件正文的替代形式的附件集合。

Attachments

获取用于存储附加到此电子邮件的数据的附件集合。

Bcc

获取包含此电子邮件密件抄送 (BCC) 收件人的地址集合。

Body

获取或设置邮件正文。

BodyEncoding

获取或设置用于邮件正文的编码。

BodyTransferEncoding

获取或设置用于邮件正文的传输编码。

CC

获取包含此电子邮件抄送 (CC) 收件人的地址集合。

DeliveryNotificationOptions

获取或设置此电子邮件的发送通知。

From

获取或设置此电子邮件的发件人地址。

Headers

获取与此电子邮件一起传输的电子邮件标头。

HeadersEncoding

获取或设置此电子邮件的用户自定义标头使用的编码。

IsBodyHtml

获取或设置一个值,指示邮件正文是否为 HTML 格式。

Priority

获取或设置此电子邮件的优先级。

ReplyTo
已过时。
已过时。
已过时。
已过时。

获取或设置邮件的回复地址。

ReplyToList

获取邮件的回复地址的列表。

Sender

获取或设置此电子邮件的发件人地址。

Subject

获取或设置此电子邮件的主题行。

SubjectEncoding

获取或设置用于此电子邮件主题内容的编码。

To

获取包含此电子邮件收件人的地址集合。

方法

Dispose()

释放由 MailMessage 使用的所有资源。

Dispose(Boolean)

释放由 MailMessage 占用的非托管资源,还可以另外再释放托管资源。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于