Share via


SmtpClient.Timeout Properti

Definisi

Mendapatkan atau menetapkan nilai yang menentukan jumlah waktu setelah waktu panggilan sinkron Send habis.

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

Nilai Properti

Yang Int32 menentukan nilai waktu habis dalam milidetik. Nilai defaultnya adalah 100.000 (100 detik).

Pengecualian

Nilai yang ditentukan untuk operasi set kurang dari nol.

Anda tidak dapat mengubah nilai properti ini ketika email sedang dikirim.

Contoh

Contoh kode berikut menunjukkan mendapatkan dan mengatur nilai waktu habis.

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

Keterangan

Secara default, panggilan ke Send blok metode hingga operasi selesai. Jika Anda mengatur Timeout properti ke nilai positif, dan operasi tidak dapat diselesaikan dalam waktu yang Send dialokasikan, SmtpClient kelas akan memberikan SmtpException pengecualian.

Untuk mengirim pesan dan melanjutkan eksekusi di utas aplikasi, gunakan metode .SendAsync

Berlaku untuk