SmtpClient 构造函数

定义

初始化 SmtpClient 类的新实例。

重载

SmtpClient()

使用配置文件设置来初始化 SmtpClient 类的新实例。

SmtpClient(String)

初始化 SmtpClient 类的新实例,该类使用指定的 SMTP 服务器发送电子邮件。

SmtpClient(String, Int32)

初始化 SmtpClient 类的新实例,该类使用指定的 SMTP 服务器和端口发送电子邮件。

SmtpClient()

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

使用配置文件设置来初始化 SmtpClient 类的新实例。

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

示例

下面的代码示例演示如何发送电子邮件。

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> 元素 (网络设置)

注解

此构造函数使用应用程序或计算机配置文件中的设置初始化HostCredentialsSmtpClient 的 、 和 Port 属性。 有关详细信息,请参阅 <mailSettings> 元素 (网络设置)

适用于

SmtpClient(String)

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

初始化 SmtpClient 类的新实例,该类使用指定的 SMTP 服务器发送电子邮件。

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)

参数

host
String

一个包含用于 SMTP 事务的主机名或主机 IP 地址的 String

示例

下面的代码示例演示如何调用此构造函数。

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 用于初始化 属性的值 HostCredentialsPort 属性通过使用应用程序或计算机配置文件中的设置进行初始化。 如果 hostnull 或等于 String.EmptyHost 则使用应用程序或计算机配置文件中的设置进行初始化。

有关使用应用程序和计算机配置文件的详细信息,请参阅 <mailSettings> 元素 (网络设置) 。 如果使用构造函数或属性指定 SmtpClient 信息,则此信息将替代配置文件设置。

适用于

SmtpClient(String, Int32)

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

初始化 SmtpClient 类的新实例,该类使用指定的 SMTP 服务器和端口发送电子邮件。

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)

参数

host
String

一个包含用于 SMTP 事务的主机名或主机 IP 地址的 String

port
Int32

大于 0 的 Int32,包含要在 host 上使用的端口。

例外

port 不能小于零。

示例

下面的代码示例演示如何调用此构造函数。

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);
}

注解

hostport 参数分别设置 和 Port 属性的值Host。 如果 hostnull 或等于 String.EmptyHost 则使用应用程序或计算机配置文件中的设置进行初始化。 如果 port 为零, Port 则使用应用程序或计算机配置文件中的设置进行初始化。 使用 Credentials 应用程序或计算机配置文件中的设置初始化 属性。

有关使用应用程序和计算机配置文件的详细信息,请参阅 <mailSettings> 元素 (网络设置) 。 如果使用构造函数或属性指定 SmtpClient 信息,则此信息将替代配置文件设置。

适用于