SmtpClient.Host 屬性

定義

取得或設定用於 SMTP 交易的主機名稱或 IP 位址。Gets or sets the name or IP address of the host used for SMTP transactions.

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

String,包含用於 SMTP 交易的電腦名稱或 IP 位址。A String that contains the name or IP address of the computer to use for SMTP transactions.

例外狀況

為設定作業指定的值為 nullThe value specified for a set operation is null.

為設定作業指定的值等於 Empty ("")。The value specified for a set operation is equal to Empty ("").

正在傳送電子郵件時,無法變更這個屬性的值。You cannot change the value of this property when an email is being sent.

範例

下列程式碼範例示範如何使用應用程式佈建檔中指定的主機和埠來傳送電子郵件訊息。The following code example demonstrates sending an email message by using the host and port specified in an application configuration file.

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 也可以使用函式或應用程式或電腦設定檔來設定屬性的值。The value of the Host property can also be set using constructors or the application or machine configuration file. 如需詳細資訊,請參閱 < > (網路設定) 的 mailSettings 元素For more information, see <mailSettings> Element (Network Settings).

如果使用此屬性指定資訊,此資訊會覆寫設定檔設定。If information is specified using this property, this information overrides the configuration file settings.

適用於

另請參閱