System.Net.Mail.SmtpClient.SendAsync Method

Sends an e-mail message to an SMTP server for delivery. The message sender, recipients, subject, and message body are specified using string objects. 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 (string from, string to, string subject, string body, object userToken)

Parameters

to
Documentation for this section has not yet been entered.
from
A string that contains the address information of the message sender.
subject
A string that contains the subject line for the message.
body
A string that contains the message body.
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 properties must be set either through the configuration files or by setting the properties or 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 property.

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