MailAddress 构造函数

定义

初始化 MailAddress 类的新实例。

重载

MailAddress(String)

使用指定的地址序列化 MailAddress 类的新实例。

MailAddress(String, String)

使用指定的地址和显示名称序列化 MailAddress 类的新实例。

MailAddress(String, String, Encoding)

使用指定的地址、显示名和编码初始化 MailAddress 类的新实例。

MailAddress(String)

Source:
MailAddress.cs
Source:
MailAddress.cs
Source:
MailAddress.cs

使用指定的地址序列化 MailAddress 类的新实例。

public:
 MailAddress(System::String ^ address);
public MailAddress (string address);
new System.Net.Mail.MailAddress : string -> System.Net.Mail.MailAddress
Public Sub New (address As String)

参数

address
String

一个 String,它包含一个电子邮件地址。

例外

addressnull

addressEmpty ("")。

address 的格式无法识别。

示例

下面的代码示例使用此构造函数为Bcc电子邮件的收件人创建 MailAddress 对象。

static void CreateBccTestMessage( 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 = L"Using the SmtpClient class.";
   message->Body = L"Using this feature, you can send an email message from an application very easily.";
   MailAddress^ bcc = gcnew MailAddress( L"manager1@contoso.com" );
   message->Bcc->Add( bcc );
   SmtpClient^ client = gcnew SmtpClient( server );
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   Console::WriteLine( L"Sending an email message to {0} and {1}.", to->DisplayName, message->Bcc );
   try
   { 
       client->Send( message );
   }
   catch ( Exception^ ex )
   {
       Console::WriteLine(L"Exception caught in CreateBccTestMessage(): {0}", 
                     ex->ToString() );
   }
   client->~SmtpClient();
}
public static void CreateBccTestMessage(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.Body = @"Using this feature, you can send an email message from an application very easily.";
    MailAddress bcc = new MailAddress("manager1@contoso.com");
    message.Bcc.Add(bcc);
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    Console.WriteLine("Sending an email message to {0} and {1}.",
        to.DisplayName, message.Bcc.ToString());

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
            ex.ToString());
    }
}

注解

如果将地址括在尖括号中,参数 address 可以包含显示名称和关联的电子邮件地址。 例如:

"Tom Smith <tsmith@contoso.com>"

显示名称和尖括号之间允许有空格。

下表显示了使用前面的示例地址构造的 对象的属性值 MailAddress

Property
DisplayName "Tom Smith"
Host "contoso.com"
User "tsmith"
Address "tsmith@contoso.com"

适用于

MailAddress(String, String)

Source:
MailAddress.cs
Source:
MailAddress.cs
Source:
MailAddress.cs

使用指定的地址和显示名称序列化 MailAddress 类的新实例。

public:
 MailAddress(System::String ^ address, System::String ^ displayName);
public MailAddress (string address, string? displayName);
public MailAddress (string address, string displayName);
new System.Net.Mail.MailAddress : string * string -> System.Net.Mail.MailAddress
Public Sub New (address As String, displayName As String)

参数

address
String

一个 String,它包含一个电子邮件地址。

displayName
String

一个 String,它包含与 address 相关联的显示名称。 此参数可以为 null

例外

addressnull

addressEmpty ("")。

address 的格式无法识别。

address 包含非 ASCII 字符。

示例

下面的代码示例使用此构造函数为电子邮件的发件人和收件人创建 MailAddress 实例。

static void CreateBccTestMessage( 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 = L"Using the SmtpClient class.";
   message->Body = L"Using this feature, you can send an email message from an application very easily.";
   MailAddress^ bcc = gcnew MailAddress( L"manager1@contoso.com" );
   message->Bcc->Add( bcc );
   SmtpClient^ client = gcnew SmtpClient( server );
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   Console::WriteLine( L"Sending an email message to {0} and {1}.", to->DisplayName, message->Bcc );
   try
   { 
       client->Send( message );
   }
   catch ( Exception^ ex )
   {
       Console::WriteLine(L"Exception caught in CreateBccTestMessage(): {0}", 
                     ex->ToString() );
   }
   client->~SmtpClient();
}
public static void CreateBccTestMessage(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.Body = @"Using this feature, you can send an email message from an application very easily.";
    MailAddress bcc = new MailAddress("manager1@contoso.com");
    message.Bcc.Add(bcc);
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    Console.WriteLine("Sending an email message to {0} and {1}.",
        to.DisplayName, message.Bcc.ToString());

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
            ex.ToString());
    }
}

注解

保留显示名称中的前导空格和尾随空格。

如果 displayName 包含非 ASCII 字符,则使用 iso-8859-1 字符集进行 displayName 编码。 RFC 1522 中讨论了对非 ASCII 字符进行编码,可在 中获取 https://www.ietf.org/

如果 address 包含显示名称,并且 displayNamenull 为 且 不等于 String.EmptydisplayName 则 重写 中指定的 address值。

适用于

MailAddress(String, String, Encoding)

Source:
MailAddress.cs
Source:
MailAddress.cs
Source:
MailAddress.cs

使用指定的地址、显示名和编码初始化 MailAddress 类的新实例。

public:
 MailAddress(System::String ^ address, System::String ^ displayName, System::Text::Encoding ^ displayNameEncoding);
public MailAddress (string address, string? displayName, System.Text.Encoding? displayNameEncoding);
public MailAddress (string address, string displayName, System.Text.Encoding displayNameEncoding);
new System.Net.Mail.MailAddress : string * string * System.Text.Encoding -> System.Net.Mail.MailAddress
Public Sub New (address As String, displayName As String, displayNameEncoding As Encoding)

参数

address
String

一个 String,它包含一个电子邮件地址。

displayName
String

String,包含与 address 关联的显示名。

displayNameEncoding
Encoding

Encoding,定义用于 displayName 的字符集。

例外

addressnull

displayNamenull

addressEmpty ("")。

displayNameEmpty ("")。

address 的格式无法识别。

address 包含非 ASCII 字符。

示例

下面的代码示例使用此构造函数为电子邮件的发件人创建 MailAddress 实例。

// Create a mailing address that includes a UTF8 
// character in the display name.
MailAddress^ from = gcnew MailAddress("jane@contoso.com",
    "Jane " + (wchar_t)0xD8 + " Clayton",
    System::Text::Encoding::UTF8);
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("jane@contoso.com",
   "Jane " + (char)0xD8+ " Clayton",
System.Text.Encoding.UTF8);
' Create a mailing address that includes a UTF8 character
' in the display name.
Dim mailFrom As New MailAddress("jane@contoso.com", "Jane " & ChrW(&HD8) & " Clayton", System.Text.Encoding.UTF8)

注解

保留显示名称中的前导空格和尾随空格。

如果 address 包含显示名称,并且 displayNamenull 为 且 不等于 String.EmptydisplayName 则 重写 中指定的 address值。

MailAddress如果 参数有效, displayName 方法不会检查。 此方法删除属性未显示的括在引号内 DisplayName 。 除非在 参数中displayNameEncoding指定了 或 UnicodeASCII否则将在传输之前添加引号。 参数中指定的displayNameEncoding编码将在传输ASCII之前应用于 DisplayName 属性,或在 Unicode 参数中displayNameEncoding指定。 UTF8 如果未指定,则为默认编码。

有关支持的邮件地址格式的详细信息,请参阅 MailAddress

适用于