英語で読む

次の方法で共有


SmtpClient.Timeout プロパティ

定義

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

public int Timeout { get; set; }

プロパティ値

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

例外

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

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

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

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

注釈

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

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

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1