SmtpClient.Host 属性

定义

获取或设置用于 SMTP 事务的主机名或主机 IP 地址。

public:
 property System::String ^ Host { System::String ^ get(); void set(System::String ^ value); };
public string? Host { get; set; }
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();
}
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> 元素 (网络设置)

如果使用此属性指定信息,则此信息将替代配置文件设置。

适用于

另请参阅