共用方式為


SmtpClient 建構函式

定義

初始化 SmtpClient 類別的新執行個體。

多載

SmtpClient()

使用組態檔設定,初始化 SmtpClient 類別的新執行個體。

SmtpClient(String)

初始化 SmtpClient 類別的新執行個體,該類別使用指定的 SMTP 伺服器來傳送電子郵件。

SmtpClient(String, Int32)

初始化 SmtpClient 類別的新執行個體,該類別使用指定的 SMTP 伺服器和連接埠來傳送電子郵件。

SmtpClient()

來源:
SmtpClient.cs
來源:
SmtpClient.cs
來源:
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>元素 (網路設定)

備註

這個建構函式會使用應用程式或計算機組態檔中的設定,初始化HostCredentials新的 SmtpClient 、 和 Port 屬性。 如需詳細資訊,請參閱 <mailSettings> 元素 (網路設定)

適用於

SmtpClient(String)

來源:
SmtpClient.cs
來源:
SmtpClient.cs
來源:
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

String,包含用於 SMTP 交易的主機電腦名稱或 IP 位址。

範例

下列程式代碼範例示範如何呼叫這個建構函式。

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)

來源:
SmtpClient.cs
來源:
SmtpClient.cs
來源:
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

String,包含用於 SMTP 交易的主機名稱或 IP 位址。

port
Int32

大於零的 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);
}

備註

host和參數分別設定 和 portPort 屬性的值Host。 如果 是 hostnull 或等於 String.EmptyHost 則會使用應用程式或計算機組態檔中的設定來初始化。 如果 port 為零, Port 則會使用應用程式或計算機組態檔中的設定初始化。 屬性 Credentials 是使用應用程式或計算機組態檔中的設定初始化。

如需使用應用程式和電腦組態檔的詳細資訊,請參閱 <mailSettings> 元素 (網路設定) 。 如果使用建構函式或屬性指定 SmtpClient 資訊,這項資訊會覆寫組態檔設定。

適用於