SmtpClient.SendMailAsync Method

Definition

Sends the specified message to an SMTP server for delivery as an asynchronous operation.

Overloads

SendMailAsync(MailMessage)

Sends the specified message to an SMTP server for delivery as an asynchronous operation.

SendMailAsync(MailMessage, CancellationToken)

Sends the specified message to an SMTP server for delivery as an asynchronous operation.

SendMailAsync(String, String, String, String)

Sends the specified message to an SMTP server for delivery as an asynchronous operation. The message sender, recipients, subject, and message body are specified using String objects.

SendMailAsync(String, String, String, String, CancellationToken)

Sends the specified message to an SMTP server for delivery as an asynchronous operation, using the specified sender, recipients, subject, and body strings.

SendMailAsync(MailMessage)

Source:
SmtpClient.cs
Source:
SmtpClient.cs
Source:
SmtpClient.cs

Sends the specified message to an SMTP server for delivery as an asynchronous operation.

public:
 System::Threading::Tasks::Task ^ SendMailAsync(System::Net::Mail::MailMessage ^ message);
public System.Threading.Tasks.Task SendMailAsync (System.Net.Mail.MailMessage message);
member this.SendMailAsync : System.Net.Mail.MailMessage -> System.Threading.Tasks.Task
Public Function SendMailAsync (message As MailMessage) As Task

Parameters

message
MailMessage

A MailMessage that contains the message to send.

Returns

The task object representing the asynchronous operation.

Exceptions

message is null.

This SmtpClient has another send operation already in progress.

-or-

There are no recipients specified in To, CC, and Bcc properties.

-or-

DeliveryMethod property is set to Network and Host is null.

-or-

DeliveryMethod property is set to Network and Host is equal to the empty string ("").

-or-

DeliveryMethod property is set to Network and Port is zero, a negative number, or greater than 65,535.

This object has been disposed.

The connection to the SMTP server failed.

-or-

Authentication failed.

-or-

The operation timed out.

-or-

EnableSsl is set to true but the DeliveryMethod property is set to SpecifiedPickupDirectory or PickupDirectoryFromIis.

-or-

EnableSsl is set to true, but the SMTP mail server did not advertise STARTTLS in the response to the EHLO command.

-or-

The message could not be delivered to one or more of the recipients in To, CC, or Bcc.

The message could not be delivered to one of the recipients in To, CC, or Bcc.

The message could not be delivered to two or more of the recipients in To, CC, or Bcc.

Remarks

This operation will not block. The returned Task object will complete once the message has been sent.

Applies to

SendMailAsync(MailMessage, CancellationToken)

Source:
SmtpClient.cs
Source:
SmtpClient.cs
Source:
SmtpClient.cs

Sends the specified message to an SMTP server for delivery as an asynchronous operation.

public:
 System::Threading::Tasks::Task ^ SendMailAsync(System::Net::Mail::MailMessage ^ message, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task SendMailAsync (System.Net.Mail.MailMessage message, System.Threading.CancellationToken cancellationToken);
member this.SendMailAsync : System.Net.Mail.MailMessage * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function SendMailAsync (message As MailMessage, cancellationToken As CancellationToken) As Task

Parameters

message
MailMessage

The message to send.

cancellationToken
CancellationToken

The cancellation token to cancel the operation.

Returns

The task object representing the asynchronous operation.

Exceptions

message is null.

This SmtpClient has another send operation already in progress.

-or-

From is null.

-or-

There are no recipients specified in To, CC, and Bcc properties.

-or-

DeliveryMethod property is set to Network and Host is null.

-or-

DeliveryMethod property is set to Network and Host is equal to the empty string ("").

-or-

DeliveryMethod property is set to Network and Port is zero, a negative number, or greater than 65,535.

This object has been disposed.

The connection to the SMTP server failed.

-or-

Authentication failed.

-or-

The operation timed out.

-or-

EnableSsl is set to true but the DeliveryMethod property is set to SpecifiedPickupDirectory or PickupDirectoryFromIis.

-or-

EnableSsl is set to true, but the SMTP mail server did not advertise STARTTLS in the response to the EHLO command.

-or-

The message could not be delivered to one or more of the recipients in To, CC, or Bcc.

The message could not be delivered to one of the recipients in To, CC, or Bcc.

The message could not be delivered to two or more of the recipients in To, CC, or Bcc.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This operation will not block. The returned Task object will complete once the message has been sent.

Applies to

SendMailAsync(String, String, String, String)

Source:
SmtpClient.cs
Source:
SmtpClient.cs
Source:
SmtpClient.cs

Sends the specified message to an SMTP server for delivery as an asynchronous operation. The message sender, recipients, subject, and message body are specified using String objects.

public:
 System::Threading::Tasks::Task ^ SendMailAsync(System::String ^ from, System::String ^ recipients, System::String ^ subject, System::String ^ body);
public System.Threading.Tasks.Task SendMailAsync (string from, string recipients, string? subject, string? body);
public System.Threading.Tasks.Task SendMailAsync (string from, string recipients, string subject, string body);
member this.SendMailAsync : string * string * string * string -> System.Threading.Tasks.Task
Public Function SendMailAsync (from As String, recipients As String, subject As String, body As String) As Task

Parameters

from
String

A String that contains the address information of the message sender.

recipients
String

A String that contains the addresses that the message is sent to.

subject
String

A String that contains the subject line for the message.

body
String

A String that contains the message body.

Returns

The task object representing the asynchronous operation.

Exceptions

from is null.

-or-

recipients is null.

from is Empty.

-or-

recipients is Empty.

This SmtpClient has another send operation already in progress.

-or-

From is null.

-or-

DeliveryMethod property is set to Network and Host is null.

-or-

DeliveryMethod property is set to Network and Host is equal to the empty string ("").

-or-

DeliveryMethod property is set to Network and Port is zero, a negative number, or greater than 65,535.

This object has been disposed.

The connection to the SMTP server failed.

-or-

Authentication failed.

-or-

The operation timed out.

-or-

EnableSsl is set to true but the DeliveryMethod property is set to SpecifiedPickupDirectory or PickupDirectoryFromIis.

-or-

EnableSsl is set to true, but the SMTP mail server did not advertise STARTTLS in the response to the EHLO command.

-or-

The message could not be delivered to one or more of the recipients in To, CC, or Bcc.

The message could not be delivered to one of the recipients in To, CC, or Bcc.

The message could not be delivered to two or more of the recipients in To, CC, or Bcc.

Remarks

This operation will not block. The returned Task object will complete once the message has been sent.

Applies to

SendMailAsync(String, String, String, String, CancellationToken)

Source:
SmtpClient.cs
Source:
SmtpClient.cs
Source:
SmtpClient.cs

Sends the specified message to an SMTP server for delivery as an asynchronous operation, using the specified sender, recipients, subject, and body strings.

public:
 System::Threading::Tasks::Task ^ SendMailAsync(System::String ^ from, System::String ^ recipients, System::String ^ subject, System::String ^ body, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task SendMailAsync (string from, string recipients, string? subject, string? body, System.Threading.CancellationToken cancellationToken);
member this.SendMailAsync : string * string * string * string * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function SendMailAsync (from As String, recipients As String, subject As String, body As String, cancellationToken As CancellationToken) As Task

Parameters

from
String

The address information of the message sender.

recipients
String

The addresses that the message is sent to.

subject
String

The subject line for the message.

body
String

The message body.

cancellationToken
CancellationToken

The cancellation token to cancel the operation.

Returns

The task object representing the asynchronous operation.

Exceptions

from is null.

-or-

recipients is null.

from is Empty.

-or-

recipients is Empty.

This SmtpClient has another send operation already in progress.

-or-

DeliveryMethod property is set to Network and Host is null.

-or-

DeliveryMethod property is set to Network and Host is equal to the empty string ("").

-or-

DeliveryMethod property is set to Network and Port is zero, a negative number, or greater than 65,535.

This object has been disposed.

The connection to the SMTP server failed.

-or-

Authentication failed.

-or-

The operation timed out.

-or-

EnableSsl is set to true but the DeliveryMethod property is set to SpecifiedPickupDirectory or PickupDirectoryFromIis.

-or-

EnableSsl is set to true, but the SMTP mail server did not advertise STARTTLS in the response to the EHLO command.

-or-

The message could not be delivered to one or more of the recipients in To, CC, or Bcc.

The message could not be delivered to one of the recipients in To, CC, or Bcc.

The message could not be delivered to two or more of the recipients in To, CC, or Bcc.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This operation will not block. The returned Task object will complete once the message has been sent.

Applies to