共用方式為


SmtpClient.Timeout 屬性

定義

取得或設定值,指定同步 Send 呼叫逾時的時間長度。

public:
 property int Timeout { int get(); void set(int value); };
public int Timeout { get; set; }
member this.Timeout : int with get, set
Public Property Timeout As Integer

屬性值

Int32,指定以毫秒為單位的逾時值。 預設值為 100,000 (100 秒)。

例外狀況

為設定作業指定的值小於零。

正在傳送電子郵件時,無法變更這個屬性的值。

範例

下列程式代碼範例示範取得和設定逾時值。

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

備註

根據預設,呼叫 Send 方法區塊,直到作業完成為止。 如果您將 Timeout 屬性設定為正值,而且 Send 作業無法在配置的時間內完成,則 SmtpClient 類別會 SmtpException 擲回例外狀況。

若要傳送訊息並繼續在應用程式線程中執行,請使用 SendAsync 方法。

適用於