SmtpClient.Port 屬性


取得或設定用於 SMTP 交易的連接埠。Gets or sets the port used for SMTP transactions.

 property int Port { int get(); void set(int value); };
public int Port { get; set; }
member this.Port : int with get, set
Public Property Port As Integer



Int32,包含 SMTP 主機上的連接埠編號。An Int32 that contains the port number on the SMTP host. 預設值為 25。The default value is 25.


為設定作業指定的值小於或等於零。The value specified for a set operation is less than or equal to zero.

正在傳送電子郵件時,無法變更這個屬性的值。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"" );
   MailAddress^ to = gcnew MailAddress( L"" );
   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 );

public static void CreateTestMessage4(string server)
    MailAddress from = new MailAddress("");
    MailAddress to = new MailAddress("");
    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);

    catch (Exception ex)
        Console.WriteLine("Exception caught in CreateTestMessage4(): {0}",


Port 也可以使用函式或應用程式或電腦設定檔來設定屬性的值。The value of the Port property can also be set using constructors or the application or machine configuration file. 如需使用設定檔的詳細資訊,請參閱 < > (網路設定) 的 mailSettings 元素For more information about using configuration files, see <mailSettings> Element (Network Settings). 如果使用此屬性指定資訊,此資訊會覆寫設定檔設定。If information is specified using this property, this information overrides the configuration file settings.