Click or drag to resize
MimeKit

SmtpClientSendAsync(FormatOptions, MimeMessage, CancellationToken, ITransferProgress) Method

Asynchronously send the specified message.

Namespace: MailKit.Net.Smtp
Assembly: MailKit (in MailKit.dll) Version: 4.3.0
Syntax
C#
public override Task<string> SendAsync(
	FormatOptions options,
	MimeMessage message,
	CancellationToken cancellationToken = default,
	ITransferProgress progress = null
)

Parameters

options  FormatOptions
The formatting options.
message  MimeMessage
The message.
cancellationToken  CancellationToken  (Optional)
The cancellation token.
progress  ITransferProgress  (Optional)
The progress reporting mechanism.

Return Value

TaskString
The final free-form text response from the server.

Implements

IMailTransportSendAsync(FormatOptions, MimeMessage, CancellationToken, ITransferProgress)
IMailTransportSendAsync(FormatOptions, MimeMessage, CancellationToken, ITransferProgress)
Exceptions
ExceptionCondition
ArgumentNullException

options is null.

-or-

message is null.

ObjectDisposedException The SmtpClient has been disposed.
ServiceNotConnectedException The SmtpClient is not connected.
ServiceNotAuthenticatedException Authentication is required before sending a message.
InvalidOperationException

A sender has not been specified.

-or-

No recipients have been specified.

NotSupportedException Internationalized formatting was requested but is not supported by the server.
OperationCanceledException The operation has been canceled.
IOException An I/O error occurred.
SmtpCommandException The SMTP command failed.
SmtpProtocolException An SMTP protocol exception occurred.
Remarks

Sends the specified message.

The sender address is determined by checking the following message headers (in order of precedence): Resent-Sender, Resent-From, Sender, and From.

If either the Resent-Sender or Resent-From addresses are present, the recipients are collected from the Resent-To, Resent-Cc, and Resent-Bcc headers, otherwise the To, Cc, and Bcc headers are used.

Example
C#
public static void SendMessageWithOptions (MimeMessage message)
{
    using (var client = new SmtpClient ()) {
        client.Connect ("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);

        client.Authenticate ("username", "password");

        var options = FormatOptions.Default.Clone ();

        if (client.Capabilities.HasFlag (SmtpCapabilities.UTF8))
            options.International = true;

        client.Send (options, message);

        client.Disconnect (true);
    }
}
See Also