SmtpClient 构造函数

定义

初始化 SmtpClient 类的新实例。Initializes a new instance of the SmtpClient class.

重载

SmtpClient()

使用配置文件设置来初始化 SmtpClient 类的新实例。Initializes a new instance of the SmtpClient class by using configuration file settings.

SmtpClient(String)

初始化 SmtpClient 类的新实例,该类使用指定的 SMTP 服务器发送电子邮件。Initializes a new instance of the SmtpClient class that sends email by using the specified SMTP server.

SmtpClient(String, Int32)

初始化 SmtpClient 类的新实例,该类使用指定的 SMTP 服务器和端口发送电子邮件。Initializes a new instance of the SmtpClient class that sends email by using the specified SMTP server and port.

SmtpClient()

使用配置文件设置来初始化 SmtpClient 类的新实例。Initializes a new instance of the SmtpClient class by using configuration file settings.

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

示例

下面的代码示例演示如何发送电子邮件。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);
}

有关应用程序或计算机配置文件中 <mailSettings > 节点的示例,请参阅<mailSettings> 元素(网络设置)For an example of the <mailSettings> node in the application or machine configuration file, see <mailSettings> Element (Network Settings).

注解

此构造函数通过使用应用程序或计算机配置文件中的设置来初始化新的 SmtpClientHostCredentialsPort 属性。This constructor initializes the Host, Credentials, and Port properties for the new SmtpClient by using the settings in the application or machine configuration files. 有关详细信息,请参阅<mailSettings> 元素(网络设置)For more information, see <mailSettings> Element (Network Settings).

SmtpClient(String)

初始化 SmtpClient 类的新实例,该类使用指定的 SMTP 服务器发送电子邮件。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);
new System.Net.Mail.SmtpClient : string -> System.Net.Mail.SmtpClient
Public Sub New (host As String)

参数

host
String

一个包含用于 SMTP 事务的主机名或主机 IP 地址的 StringA String that contains the name or IP address of the host computer used for SMTP transactions.

示例

下面的代码示例演示如何调用此构造函数。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);
}

注解

host 参数用于初始化 Host 属性的值。The host parameter is used to initialize the value of the Host property. 使用应用程序或计算机配置文件中的设置初始化 CredentialsPort 属性。The Credentials and Port properties are initialized by using the settings in the application or machine configuration files. 如果 host null 或等于 String.Empty,则使用应用程序或计算机配置文件中的设置初始化 HostIf host is null or equal to String.Empty, Host is initialized using the settings in the application or machine configuration files.

有关使用应用程序和计算机配置文件的详细信息,请参阅<mailSettings> 元素(网络设置)For more information about using the application and machine configuration files, see <mailSettings> Element (Network Settings). 如果使用 SmtpClient 构造函数或属性指定信息,则此信息将重写配置文件设置。If information is specified using SmtpClient constructors or properties, this information overrides the configuration file settings.

SmtpClient(String, Int32)

初始化 SmtpClient 类的新实例,该类使用指定的 SMTP 服务器和端口发送电子邮件。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);
new System.Net.Mail.SmtpClient : string * int -> System.Net.Mail.SmtpClient
Public Sub New (host As String, port As Integer)

参数

host
String

一个包含用于 SMTP 事务的主机名或主机 IP 地址的 StringA String that contains the name or IP address of the host used for SMTP transactions.

port
Int32

大于 0 的 Int32,包含要在 host 上使用的端口。An Int32 greater than zero that contains the port to be used on host.

例外

port 不能小于零。port cannot be less than zero.

示例

下面的代码示例演示如何调用此构造函数。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;
   
   //Display the properties on the service point.
   ServicePoint^ p = client->ServicePoint;
   Console::WriteLine( L"Connection lease timeout: {0}", p->ConnectionLeaseTimeout );
   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);
}

注解

hostport 参数分别设置 HostPort 属性的值。The host and port parameters set the value of the Host and Port properties, respectively. 如果 host null 或等于 String.Empty,则使用应用程序或计算机配置文件中的设置初始化 HostIf host is null or equal to String.Empty, Host is initialized using the settings in the application or machine configuration files. 如果 port 为零,Port 将使用应用程序或计算机配置文件中的设置进行初始化。If port is zero, Port is initialized using the settings in the application or machine configuration files. 使用应用程序或计算机配置文件中的设置初始化 Credentials 属性。The Credentials property is initialized using the settings in the application or machine configuration files.

有关使用应用程序和计算机配置文件的详细信息,请参阅<mailSettings> 元素(网络设置)For more information about using the application and machine configuration files, see <mailSettings> Element (Network Settings). 如果使用 SmtpClient 构造函数或属性指定信息,则此信息将重写配置文件设置。If information is specified using SmtpClient constructors or properties, this information overrides the configuration file settings.

适用于