.NET Framework クラス ライブラリ
SmtpClient..::.Timeout プロパティ

更新 : 2007 年 11 月

同期的な Send 呼び出しがタイムアウトになるまでの時間を指定する値を取得または設定します。

名前空間 :  System.Net.Mail
アセンブリ :  System (System.dll 内)

構文

Visual Basic (宣言)
Public Property Timeout As Integer
Visual Basic (使用法)
Dim instance As SmtpClient
Dim value As Integer

value = instance.Timeout

instance.Timeout = value
C#
public int Timeout { get; set; }
Visual C++
public:
property int Timeout {
    int get ();
    void set (int value);
}
J#
/** @property */
public int get_Timeout()
/** @property */
public  void set_Timeout(int value)
JScript
public function get Timeout () : int
public function set Timeout (value : int)

プロパティ値

型 : System..::.Int32

タイムアウト値 (ミリ秒) を指定する Int32。既定値は 100,000 ミリ秒 (100 秒) です。

例外

例外条件
ArgumentOutOfRangeException

設定操作として指定した値が 0 未満です。

InvalidOperationException

電子メールが送信中の場合、このプロパティの値は変更できません。

解説

既定では、Send メソッドを呼び出すと、その操作が完了するまでブロックされます。Timeout プロパティが正の値に設定されていて Send 操作が指定の時間内に完了できなかった場合は、SmtpClient クラスによって SmtpException 例外がスローされます。

メッセージを送信し、同時にアプリケーション スレッドで実行を継続するには、SendAsync メソッドを使用します。


タイムアウト値の取得と設定を行うコード例を次に示します。

C#
        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 e-mail 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 e-mail on the client's behalf.
            client.Credentials = CredentialCache.DefaultNetworkCredentials;
            client.Send(message);
        }
Visual C++
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 e-mail 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 e-mail on the client's behalf.
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   client->Send( message );
}


J#
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 e-mail 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.", 
        System.Convert.ToString(client.get_Timeout()));
    client.set_Timeout(100);
    // Credentials are necessary if the server requires the client 
    // to authenticate before it will send e-mail on the client's behalf.
    client.set_Credentials(CredentialCache.get_DefaultNetworkCredentials());
    client.Send(message);
} //CreateTimeoutTestMessage
プラットフォーム

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 3.5、3.0、2.0
参照

参照

タグ :


Page view tracker