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.