Smtp ClientSend(Format Options, ISend Request, Cancellation Token) Method
Definition
Assembly: MailKit (in MailKit.dll) Version: 5.0.0
public override SendResult Send(
FormatOptions options,
ISendRequest request,
CancellationToken cancellationToken = default
)Parameters
- options FormatOptions
- The formatting options.
- request ISendRequest
- The send request.
- cancellationToken CancellationToken (Optional)
- The cancellation token.
Return Value
SendResultThe result of sending the message as an SmtpSendResult.
Implements
IMailTransportSend(FormatOptions, ISendRequest, CancellationToken)IMailTransportSend(FormatOptions, ISendRequest, CancellationToken)
Remarks
Sends the message specified by the request.
If the Sender is , the sender address is determined by checking the following message headers (in order of precedence): Resent-Sender, Resent-From, Sender, and From.
If the Recipients is , the recipients are collected from the Resent-To, Resent-Cc, and Resent-Bcc headers if either the Resent-Sender or Resent-From addresses are present, otherwise the To, Cc, and Bcc headers are used.
If the request implements ISmtpSendRequest, it may also be used to request delivery status notifications and to handle the server's response to each MAIL FROM and RCPT TO command.
Example
public static void SendWithDeliveryStatusNotifications (MimeMessage message)
{
var request = new SmtpSendRequest (message) {
// Since you will want to be able to map the envelope identifier back to the
// message, the obvious identifier to use is probably the Message-Id value.
EnvelopeId = message.MessageId,
// Request that the server include only the headers of the original message
// in any delivery status notifications that it sends back.
DeliveryStatusNotificationType = DeliveryStatusNotificationType.HeadersOnly,
// In this example, we only want to be notified of failures to deliver to a mailbox.
// If you also want to be notified of delays or successful deliveries, simply bitwise-or
// whatever combination of flags you want to be notified about.
DeliveryStatusNotifications = DeliveryStatusNotification.Failure
};
using (var client = new SmtpClient ()) {
client.Connect ("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);
client.Authenticate ("username", "password");
var result = (SmtpSendResult) client.Send (request);
Console.WriteLine ($"The server responded with: {result.StatusCode} {result.ResponseText}");
client.Disconnect (true);
}
}
public class PerRecipientSmtpSendRequest : SmtpSendRequest
{
public PerRecipientSmtpSendRequest (MimeMessage message) : base (message)
{
EnvelopeId = message.MessageId;
}
/// <summary>
/// Get the types of delivery status notification desired for the specified recipient mailbox.
/// </summary>
/// <remarks>
/// Gets the types of delivery status notification desired for the specified recipient mailbox.
/// </remarks>
/// <returns>The desired delivery status notification type.</returns>
/// <param name="recipient">The recipient mailbox.</param>
public override DeliveryStatusNotification? GetDeliveryStatusNotifications (MailboxAddress recipient)
{
// Only request delivery status notifications for recipients outside of our domain.
if (recipient.Domain.Equals ("example.com", StringComparison.OrdinalIgnoreCase))
return DeliveryStatusNotification.Never;
return DeliveryStatusNotification.Failure | DeliveryStatusNotification.Delay;
}
}
Exceptions
| ArgumentNullException |
options is . -or- request is . |
| 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. |
| SmtpUtf8AddressException | The message is being sent using the SMTPUTF8 extension and the sender or one of the recipients is not a valid SMTPUTF8 address. |
| OperationCanceledException | The operation has been canceled. |
| IOException | An I/O error occurred. |
| SmtpCommandException | The SMTP command failed. |
| SmtpProtocolException | An SMTP protocol exception occurred. |