MailAddress 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 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
为 null
。
address
为 Empty ("")。
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
为 null
。
address
为 Empty ("")。
示例
下面的代码示例使用此构造函数为电子邮件的发件人和收件人创建 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
包含显示名称,并且 displayName
不 null
为 且 不等于 String.Empty, displayName
则 重写 中指定的 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)
参数
例外
示例
下面的代码示例使用此构造函数为电子邮件的发件人创建 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
包含显示名称,并且 displayName
不 null
为 且 不等于 String.Empty, displayName
则 重写 中指定的 address
值。
MailAddress如果 参数有效, displayName
方法不会检查。 此方法删除属性未显示的括在引号内 DisplayName 。 除非在 参数中displayNameEncoding
指定了 或 Unicode ,ASCII否则将在传输之前添加引号。 参数中指定的displayNameEncoding
编码将在传输ASCII之前应用于 DisplayName 属性,或在 Unicode 参数中displayNameEncoding
指定。 UTF8 如果未指定,则为默认编码。
有关支持的邮件地址格式的详细信息,请参阅 MailAddress。