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 秒)。

例外

为设置操作指定的值小于 0。

发送电子邮件时无法更改此属性的值。

示例

下面的代码示例演示如何获取和设置超时值。

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 方法。

适用于