SmtpClient コンストラクター

定義

SmtpClient クラスの新しいインスタンスを初期化します。

オーバーロード

SmtpClient()

構成ファイルの設定を使用して SmtpClient クラスの新しいインスタンスを初期化します。

SmtpClient(String)

指定した SMTP サーバーを使用して電子メールを送信する SmtpClient クラスの新しいインスタンスを初期化します。

SmtpClient(String, Int32)

指定した SMTP サーバーとポートを使用して電子メールを送信する SmtpClient クラスのインスタンスを初期化します。

SmtpClient()

構成ファイルの設定を使用して 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> 要素 (ネットワーク設定)」を参照してください<

注釈

このコンストラクターは、Hostアプリケーションまたはコンピューター構成ファイルの設定を使用して、新しい SmtpClient の 、Credentials、および Port プロパティを初期化します。 詳細については、「mailSettings> 要素 (ネットワーク設定)」を参照してください<

適用対象

SmtpClient(String)

指定した SMTP サーバーを使用して電子メールを送信する SmtpClient クラスの新しいインスタンスを初期化します。

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 、 プロパティの Host 値を初期化するために使用されます。 Credentialsプロパティと Port プロパティは、アプリケーションまたはコンピューター構成ファイルの設定を使用して初期化されます。 が null または と等しいString.EmptyHost場合hostは、アプリケーションまたはマシン構成ファイルの設定を使用して初期化されます。

アプリケーション構成ファイルとコンピューター構成ファイルの使用の詳細については、「mailSettings> 要素 (ネットワーク設定)」を参照してください<。 コンストラクターまたはプロパティを使用して SmtpClient 情報を指定した場合、この情報は構成ファイルの設定をオーバーライドします。

適用対象

SmtpClient(String, Int32)

指定した SMTP サーバーとポートを使用して電子メールを送信する SmtpClient クラスのインスタンスを初期化します。

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

host 上で使用されるポートを格納している 1 以上の Int32

例外

port は 0 未満には設定できません。

次のコード例では、このコンストラクターの呼び出しを示します。

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

注釈

パラメーターと port パラメーターはhost、 プロパティと Port プロパティのHost値をそれぞれ設定します。 が null または と等しいString.EmptyHost場合hostは、アプリケーションまたはマシン構成ファイルの設定を使用して初期化されます。 が 0 の場合 portPort はアプリケーションまたはマシン構成ファイルの設定を使用して初期化されます。 プロパティは Credentials 、アプリケーションまたはコンピューター構成ファイルの設定を使用して初期化されます。

アプリケーション構成ファイルとコンピューター構成ファイルの使用の詳細については、「mailSettings> 要素 (ネットワーク設定)」を参照してください<。 コンストラクターまたはプロパティを使用して SmtpClient 情報を指定した場合、この情報は構成ファイルの設定をオーバーライドします。

適用対象