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).

備註

這個函式會 Host Credentials Port SmtpClient 使用應用程式或電腦設定檔中的設定,初始化新的、和屬性。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);
public SmtpClient (string host);
new System.Net.Mail.SmtpClient : string -> System.Net.Mail.SmtpClient
Public Sub New (host As String)

參數

host
String

String,包含用於 SMTP 交易的主機電腦名稱或 IP 位址。A 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參數是用來初始化屬性的值 HostThe 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.EmptyHost 則會使用應用程式或電腦設定檔中的設定來初始化。If 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);
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

String,包含用於 SMTP 交易的主機名稱或 IP 位址。A String that contains the name or IP address of the host used for SMTP transactions.

port
Int32

大於零的 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 參數會分別設定 Host 和屬性的值 PortThe host and port parameters set the value of the Host and Port properties, respectively. 如果 host 等於 null String.EmptyHost 則會使用應用程式或電腦設定檔中的設定來初始化。If 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.

適用於