Επεξεργασία

Κοινή χρήση μέσω


SmtpClient Constructors

Definition

Initializes a new instance of the SmtpClient class.

Overloads

SmtpClient()

Initializes a new instance of the SmtpClient class by using configuration file settings.

SmtpClient(String)

Initializes a new instance of the SmtpClient class that sends email by using the specified SMTP server.

SmtpClient(String, Int32)

Initializes a new instance of the SmtpClient class that sends email by using the specified SMTP server and port.

SmtpClient()

Source:
SmtpClient.cs
Source:
SmtpClient.cs
Source:
SmtpClient.cs

Initializes a new instance of the SmtpClient class by using configuration file settings.

public:
 SmtpClient();
public SmtpClient ();
Public Sub New ()

Examples

The following code example demonstrates sending an email message.

static void CreateTestMessage3()
{
   MailAddress^ to = gcnew MailAddress( L"jane@contoso.com" );
   MailAddress^ from = gcnew MailAddress( L"ben@contoso.com" );
   MailMessage^ message = gcnew MailMessage( from,to );
   message->Subject = L"Using the new SMTP client.";
   message->Body = L"Using this new feature, you can send an email message from an application very easily.";
   
   // Use the application or machine configuration to get the 
   // host, port, and credentials.
   SmtpClient^ client = gcnew SmtpClient;
   Console::WriteLine( L"Sending an email message to {0} at {1} by using the SMTP host {2}.", to->User, to->Host, client->Host );
   client->Send( message );
}
public static void CreateTestMessage3()
{
    MailAddress to = new MailAddress("jane@contoso.com");
    MailAddress from = new MailAddress("ben@contoso.com");
    MailMessage message = new MailMessage(from, to);
    message.Subject = "Using the new SMTP client.";
    message.Body = @"Using this new feature, you can send an email message from an application very easily.";
    // Use the application or machine configuration to get the
    // host, port, and credentials.
    SmtpClient client = new SmtpClient();
    Console.WriteLine("Sending an email message to {0} at {1} by using the SMTP host={2}.",
        to.User, to.Host, client.Host);
    client.Send(message);
}

For an example of the <mailSettings> node in the application or machine configuration file, see <mailSettings> Element (Network Settings).

Remarks

This constructor initializes the Host, Credentials, and Port properties for the new SmtpClient by using the settings in the application or machine configuration files. For more information, see <mailSettings> Element (Network Settings).

Applies to

SmtpClient(String)

Source:
SmtpClient.cs
Source:
SmtpClient.cs
Source:
SmtpClient.cs

Initializes a new instance of the SmtpClient class that sends email by using the specified SMTP server.

public:
 SmtpClient(System::String ^ host);
public SmtpClient (string? host);
public SmtpClient (string host);
new System.Net.Mail.SmtpClient : string -> System.Net.Mail.SmtpClient
Public Sub New (host As String)

Parameters

host
String

A String that contains the name or IP address of the host computer used for SMTP transactions.

Examples

The following code example demonstrates calling this constructor.

static void CreateTimeoutTestMessage( String^ server )
{
   String^ to = L"jane@contoso.com";
   String^ from = L"ben@contoso.com";
   String^ subject = L"Using the new SMTP client.";
   String^ body = L"Using this new feature, you can send an email message from an application very easily.";
   MailMessage^ message = gcnew MailMessage( from,to,subject,body );
   SmtpClient^ client = gcnew SmtpClient( server );
   Console::WriteLine( L"Changing time out from {0} to 100.", client->Timeout );
   client->Timeout = 100;
   
   // Credentials are necessary if the server requires the client 
   // to authenticate before it will send email on the client's behalf.
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   client->Send( message );
}
public static void CreateTimeoutTestMessage(string server)
{
    string to = "jane@contoso.com";
    string from = "ben@contoso.com";
    string subject = "Using the new SMTP client.";
    string body = @"Using this new feature, you can send an email message from an application very easily.";
    MailMessage message = new MailMessage(from, to, subject, body);
    SmtpClient client = new SmtpClient(server);
    Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
    client.Timeout = 100;
    // Credentials are necessary if the server requires the client
    // to authenticate before it will send email on the client's behalf.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    client.Send(message);
}

Remarks

The host parameter is used to initialize the value of the Host property. The Credentials and Port properties are initialized by using the settings in the application or machine configuration files. If host is null or equal to String.Empty, Host is initialized using the settings in the application or machine configuration files.

For more information about using the application and machine configuration files, see <mailSettings> Element (Network Settings). If information is specified using SmtpClient constructors or properties, this information overrides the configuration file settings.

Applies to

SmtpClient(String, Int32)

Source:
SmtpClient.cs
Source:
SmtpClient.cs
Source:
SmtpClient.cs

Initializes a new instance of the SmtpClient class that sends email by using the specified SMTP server and port.

public:
 SmtpClient(System::String ^ host, int port);
public SmtpClient (string? host, int port);
public SmtpClient (string host, int port);
new System.Net.Mail.SmtpClient : string * int -> System.Net.Mail.SmtpClient
Public Sub New (host As String, port As Integer)

Parameters

host
String

A String that contains the name or IP address of the host used for SMTP transactions.

port
Int32

An Int32 greater than zero that contains the port to be used on host.

Exceptions

port cannot be less than zero.

Examples

The following code example demonstrates calling this constructor.

static void CreateTestMessage1( String^ server, int port )
{
   String^ to = L"jane@contoso.com";
   String^ from = L"ben@contoso.com";
   String^ subject = L"Using the new SMTP client.";
   String^ body = L"Using this new feature, you can send an email message from an application very easily.";
   MailMessage^ message = gcnew MailMessage( from,to,subject,body );
   SmtpClient^ client = gcnew SmtpClient( server,port );
   
   // Credentials are necessary if the server requires the client 
   // to authenticate before it will send email on the client's behalf.
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   client->Send( message );
   client->~SmtpClient();
}
public static void CreateTestMessage1(string server, int port)
{
    string to = "jane@contoso.com";
    string from = "ben@contoso.com";
    string subject = "Using the new SMTP client.";
    string body = @"Using this new feature, you can send an email message from an application very easily.";
    MailMessage message = new MailMessage(from, to, subject, body);
    SmtpClient client = new SmtpClient(server, port);
    // Credentials are necessary if the server requires the client
    // to authenticate before it will send email on the client's behalf.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    client.Send(message);
}

Remarks

The host and port parameters set the value of the Host and Port properties, respectively. If host is null or equal to String.Empty, Host is initialized using the settings in the application or machine configuration files. If port is zero, Port is initialized using the settings in the application or machine configuration files. The Credentials property is initialized using the settings in the application or machine configuration files.

For more information about using the application and machine configuration files, see <mailSettings> Element (Network Settings). If information is specified using SmtpClient constructors or properties, this information overrides the configuration file settings.

Applies to