Edit

Share via


SmtpClient.Port Property

Definition

Gets or sets the port used for SMTP transactions.

public:
 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

Property Value

An Int32 that contains the port number on the SMTP host. The default value is 25.

Exceptions

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.

Examples

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"[email protected]" );
   MailAddress^ to = gcnew MailAddress( L"[email protected]" );
   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("[email protected]");
    MailAddress to = new MailAddress("[email protected]");
    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());
    }
}

Remarks

The value of the Port property can also be set using constructors or the application or machine configuration file. 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.

Applies to

See also