SmtpClient.Host 屬性
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定用於 SMTP 交易的主機名稱或 IP 位址。
public:
property System::String ^ Host { System::String ^ get(); void set(System::String ^ value); };
C#
public string? Host { get; set; }
C#
public string Host { get; set; }
member this.Host : string with get, set
Public Property Host As String
String,包含用於 SMTP 交易的電腦名稱或 IP 位址。
為設定作業指定的值為 null
。
為設定作業指定的值等於 Empty ("")。
正在傳送電子郵件時,無法變更這個屬性的值。
下列程式代碼範例示範如何使用應用程式組態檔中指定的主機和埠來傳送電子郵件訊息。
static void CreateTestMessage4( String^ server )
{
MailAddress^ from = gcnew MailAddress( L"ben@contoso.com" );
MailAddress^ to = gcnew MailAddress( L"Jane@contoso.com" );
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.";
SmtpClient^ client = gcnew SmtpClient( server );
Console::WriteLine( L"Sending an email message to {0} by using SMTP host {1} port {2}.", to, client->Host, client->Port );
client->Send( message );
client->~SmtpClient();
}
C#
public static void CreateTestMessage4(string server)
{
MailAddress from = new MailAddress("ben@contoso.com");
MailAddress to = new MailAddress("Jane@contoso.com");
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.";
SmtpClient client = new SmtpClient(server);
Console.WriteLine("Sending an email message to {0} by using SMTP host {1} port {2}.",
to.ToString(), client.Host, client.Port);
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage4(): {0}",
ex.ToString());
}
}
您也可以使用建構函式或應用程式或計算機組態檔來設定 屬性的值 Host 。 如需詳細資訊,請參閱 <mailSettings> 元素 (網路設定) 。
如果使用這個屬性指定資訊,這項資訊會覆寫組態檔設定。
產品 | 版本 |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |