SmtpClient.Timeout Właściwość

Definicja

Pobiera lub ustawia wartość określającą czas, po którym przekroczono limit czasu wywołania synchronicznego 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

Wartość właściwości

Element Int32 określający wartość limitu czasu w milisekundach. Wartość domyślna to 100 000 (100 sekund).

Wyjątki

Wartość określona dla operacji zestawu była mniejsza niż zero.

Nie można zmienić wartości tej właściwości podczas wysyłania wiadomości e-mail.

Przykłady

Poniższy przykład kodu przedstawia pobieranie i ustawianie wartości limitu czasu.

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

Uwagi

Domyślnie wywołuje blok metody do Send momentu zakończenia operacji. Jeśli ustawisz Timeout właściwość na wartość dodatnią, a Send operacja nie może zakończyć się w wyznaczonym czasie, SmtpClient klasa zgłasza SmtpException wyjątek.

Aby wysłać komunikat i kontynuować wykonywanie w wątku aplikacji, użyj SendAsync metody .

Dotyczy