MailAddress 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示电子邮件发件人或收件人的地址。
public ref class MailAddresspublic class MailAddresstype MailAddress = classPublic Class MailAddress- 继承
- 
				MailAddress
示例
下面的代码示例演示如何使用 SmtpClient、 MailAddress和 MailMessage 类发送电子邮件。
static void CreateCopyMessage( String^ server )
{
   MailAddress^ from = gcnew MailAddress( L"ben@contoso.com",L"Ben Miller" );
   MailAddress^ to = gcnew MailAddress( L"jane@contoso.com",L"Jane Clayton" );
   MailMessage^ message = gcnew MailMessage( from,to );
   
   // message.Subject = "Using the SmtpClient class.";
   message->Subject = L"Using the SmtpClient class.";
   message->Body = L"Using this feature, you can send an email message from an application very easily.";
   
   // Add a carbon copy recipient.
   MailAddress^ copy = gcnew MailAddress( L"Notification_List@contoso.com" );
   message->CC->Add( copy );
   SmtpClient^ client = gcnew SmtpClient( server );
   
   // Include credentials if the server requires them.
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   Console::WriteLine( L"Sending an email message to {0} by using the SMTP host {1}.", to->Address, client->Host );
   client->Send( message );
   client->~SmtpClient();
}
public static void CreateCopyMessage(string server)
{
    MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");
    MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");
    MailMessage message = new MailMessage(from, to);
    // message.Subject = "Using the SmtpClient class.";
    message.Subject = "Using the SmtpClient class.";
    message.Body = @"Using this feature, you can send an email message from an application very easily.";
    // Add a carbon copy recipient.
    MailAddress copy = new MailAddress("Notification_List@contoso.com");
    message.CC.Add(copy);
    SmtpClient client = new SmtpClient(server);
    // Include credentials if the server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    Console.WriteLine("Sending an email message to {0} by using the SMTP host {1}.",
         to.Address, client.Host);
    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateCopyMessage(): {0}",
            ex.ToString());
    }
}
注解
MailAddress和 MailMessage 类使用 SmtpClient 类来存储电子邮件的地址信息。
邮件地址由名称、Host名称和 (可选DisplayName)组成User。 DisplayName如果对非 ASCII 字符进行编码,则可以包含这些字符。
类 MailAddress 支持以下邮件地址格式:
- 的简单地址格式 - user@host。 DisplayName如果未设置 ,则这是生成的邮件地址格式。
- 标准带引号的显示名称格式 - "display name" <user@host>。 DisplayName如果设置了 ,则这是生成的格式。- 如果名称和名称Host未包含尖括号,则User添加尖括号。 例如, - "display name" user@host更改为- "display name" <user@host>。
- 如果未包含引号,则会在 DisplayName周围添加引号。 例如, - display name <user@host>更改为- "display name" <user@host>。
- 属性支持 DisplayName Unicode 字符。 
 
在这两种格式中,允许使用以下元素:
- 带 User 引号的名称。 例如 - "user name"@host。
- 用户名中的连续和尾随点。 例如 - user...name..@host。
- 用括号括起来的域文本。 例如 - <user@[my domain]>。
- 备注。 例如 - (comment)"display name"(comment)<(comment)user(comment)@(comment)domain(comment)>(comment)。 批注在传输之前被删除。
逗号用于分隔邮件地址列表中的元素。 因此,不应在列表中未加引号的显示名称中使用逗号。 允许使用以下邮件地址:
"John, Doe" <user@host>, "Bob, Smith" <user2@host>
不允许使用以下邮件地址:
John, Doe <user@host>, Bob, Smith <user2@host>
引号可以嵌入在带引号的字符串中,但必须转义。 允许使用以下邮件地址:
"John \"Jr\" Doe" <user@host>
"\"John \\\"Jr\\\" Doe\" <user@host>"
不允许使用以下邮件地址:
"John "Jr" Doe" <user@host>
如果未用引号括起用户名,则字符串开头 (或逗号) 和地址之间的所有文本都被视为 的一部分 DisplayName,包括注释。 例如:
(non comment) unquoted display (non comment) name (non comment) <user@host>
MailAddress尽管 类接受有效的邮件地址,但其他邮件服务器可能不接受该邮件地址。
类 MailAddress 不支持以下邮件地址格式:
- 带引号和无引号的混合显示名称。 例如 - display "name" <user@host>。
- 组,如 IETF 发布的 RFC 2822 第 3.4 节所定义。 
- 、 - user."name"@host或- "user".name@host的- "user"."name"@host过时用户名格式。
构造函数
| MailAddress(String) | 使用指定的地址序列化 MailAddress 类的新实例。 | 
| MailAddress(String, String) | 使用指定的地址和显示名称序列化 MailAddress 类的新实例。 | 
| MailAddress(String, String, Encoding) | 使用指定的地址、显示名和编码初始化 MailAddress 类的新实例。 | 
属性
| Address | 获取创建此实例时指定的电子邮件地址。 | 
| DisplayName | 获取由创建此实例时指定的显示名和地址信息构成的显示名。 | 
| Host | 获取创建此实例时指定的地址的主机部分。 | 
| User | 获取创建此实例时指定的地址中的用户信息。 | 
方法
| Equals(Object) | 比较两个邮件地址。 | 
| GetHashCode() | 返回邮件地址的哈希值。 | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回此实例的字符串表示形式。 | 
| TryCreate(String, MailAddress) | 创建一个新的 MailAddress。 如果无法创建对象,则不会引发异常。 | 
| TryCreate(String, String, Encoding, MailAddress) | 创建一个新的 MailAddress。 如果无法创建对象,则不会引发异常。 | 
| TryCreate(String, String, MailAddress) | 创建一个新的 MailAddress。 如果无法创建对象,则不会引发异常。 |