System.Net.Mail.SmtpClient.SendAsync Method

Sends the specified e-mail message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes.

Syntax

public void SendAsync (MailMessage message, object userToken)

Parameters

message
A System.Net.Mail.MailMessage that contains the message to send.
userToken
A user-defined object that is passed to the method invoked when the asynchronous operation completes.

Remarks

To receive notification when the e-mail has been sent or the operation has been canceled, add an event handler to the SmtpClient.SendCompleted event. You can cancel a erload:System.Net.Mail.SmtpClient.SendAsync operation by calling the SmtpClient.SendAsyncCancel method.

After calling erload:System.Net.Mail.SmtpClient.SendAsync, you must wait for the e-mail transmission to complete before attempting to send another e-mail message using erload:System.Net.Mail.SmtpClient.Send or erload:System.Net.Mail.SmtpClient.SendAsync.

Before calling this method, the SmtpClient.Host and SmtpClient.Port must be set through the configuration files by setting the relevant properties, or by passing this information into the SmtpClient.#ctor(string, int) constructor.

If the SMTP host requires credentials, you must set them before calling this method. To specify credentials, use the SmtpClient.UseDefaultCredentials or SmtpClient.Credentials properties.

If you receive an System.Net.Mail.SmtpException exception, check the SmtpException.StatusCode property to find the reason the operation failed. The System.Net.Mail.SmtpException can also contain an inner exception that indicates the reason the operation failed.

When sending e-mail using erload:System.Net.Mail.SmtpClient.SendAsync to multiple recipients, if the SMTP server accepts some recipients as valid and rejects others, a System.Net.Mail.SmtpException is thrown with a NullReferenceException for the inner exception. If this occurs, erload:System.Net.Mail.SmtpClient.SendAsync fails to send e-mail to any of the recipients.

Your application can detect a server certificate validation error by examining the System.ComponentModel.AsyncCompletedEventArgs.Error property passed into the System.Net.Mail.SendCompletedEventHandler delegate.

The SmtpClient.Timeout property does not have any effect on a erload:System.Net.Mail.SmtpClient.SendAsync call.

To send mail and block while it is transmitted to the SMTP server, use one of the erload:System.Net.Mail.SmtpClient.Send methods.

Note:

If the SmtpClient.EnableSsl property is set to true, and the SMTP mail server does not advertise STARTTLS in the response to the EHLO command, then a call to the erload:System.Net.Mail.SmtpClient.Send or erload:System.Net.Mail.SmtpClient.SendAsync methods will throw an System.Net.Mail.SmtpException.

Requirements

Namespace: System.Net.Mail
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0