// // MailTransport.cs // // Author: Jeffrey Stedfast // // Copyright (c) 2013-2020 .NET Foundation and Contributors // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // using System; using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; using MimeKit; namespace MailKit { /// /// An abstract mail transport implementation. /// /// /// An abstract mail transport implementation. /// public abstract class MailTransport : MailService, IMailTransport { static readonly FormatOptions DefaultOptions; static MailTransport () { var options = FormatOptions.Default.Clone (); options.HiddenHeaders.Add (HeaderId.ContentLength); options.HiddenHeaders.Add (HeaderId.ResentBcc); options.HiddenHeaders.Add (HeaderId.Bcc); options.NewLineFormat = NewLineFormat.Dos; DefaultOptions = options; } /// /// Initializes a new instance of the class. /// /// /// Initializes a new instance of the class. /// /// The protocol logger. /// /// is null. /// protected MailTransport (IProtocolLogger protocolLogger) : base (protocolLogger) { } /// /// Send the specified message. /// /// /// 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. /// /// /// /// /// The message. /// The cancellation token. /// The progress reporting mechanism. /// /// is null. /// /// /// The has been disposed. /// /// /// The is not connected. /// /// /// Authentication is required before sending a message. /// /// /// A sender has not been specified. /// -or- /// No recipients have been specified. /// /// /// The operation has been canceled. /// /// /// An I/O error occurred. /// /// /// The send command failed. /// /// /// A protocol exception occurred. /// public virtual void Send (MimeMessage message, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null) { Send (DefaultOptions, message, cancellationToken, progress); } /// /// Asynchronously send the specified message. /// /// /// Asynchronously 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. /// /// An asynchronous task context. /// The message. /// The cancellation token. /// The progress reporting mechanism. /// /// is null. /// /// /// The has been disposed. /// /// /// The is not connected. /// /// /// Authentication is required before sending a message. /// /// /// A sender has not been specified. /// -or- /// No recipients have been specified. /// /// /// The operation has been canceled. /// /// /// An I/O error occurred. /// /// /// The send command failed. /// /// /// A protocol exception occurred. /// public virtual Task SendAsync (MimeMessage message, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null) { return SendAsync (DefaultOptions, message, cancellationToken, progress); } /// /// Send the specified message using the supplied sender and recipients. /// /// /// Sends the specified message using the supplied sender and recipients. /// /// The message. /// The mailbox address to use for sending the message. /// The mailbox addresses that should receive the message. /// The cancellation token. /// The progress reporting mechanism. /// /// is null. /// -or- /// is null. /// -or- /// is null. /// /// /// The has been disposed. /// /// /// The is not connected. /// /// /// Authentication is required before sending a message. /// /// /// A sender has not been specified. /// -or- /// No recipients have been specified. /// /// /// The operation has been canceled. /// /// /// An I/O error occurred. /// /// /// The send command failed. /// /// /// A protocol exception occurred. /// public virtual void Send (MimeMessage message, MailboxAddress sender, IEnumerable recipients, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null) { Send (DefaultOptions, message, sender, recipients, cancellationToken, progress); } /// /// Asynchronously send the specified message using the supplied sender and recipients. /// /// /// Asynchronously sends the specified message using the supplied sender and recipients. /// /// An asynchronous task context. /// The message. /// The mailbox address to use for sending the message. /// The mailbox addresses that should receive the message. /// The cancellation token. /// The progress reporting mechanism. /// /// is null. /// -or- /// is null. /// -or- /// is null. /// /// /// The has been disposed. /// /// /// The is not connected. /// /// /// Authentication is required before sending a message. /// /// /// A sender has not been specified. /// -or- /// No recipients have been specified. /// /// /// The operation has been canceled. /// /// /// An I/O error occurred. /// /// /// The send command failed. /// /// /// A protocol exception occurred. /// public virtual Task SendAsync (MimeMessage message, MailboxAddress sender, IEnumerable recipients, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null) { return SendAsync (DefaultOptions, message, sender, recipients, cancellationToken, progress); } /// /// Send the specified message. /// /// /// 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. /// /// /// /// /// The formatting options. /// The message. /// The cancellation token. /// The progress reporting mechanism. /// /// is null. /// -or- /// is null. /// /// /// The has been disposed. /// /// /// The is not connected. /// /// /// Authentication is required before sending a message. /// /// /// A sender has not been specified. /// -or- /// No recipients have been specified. /// /// /// The operation has been canceled. /// /// /// Internationalized formatting was requested but is not supported by the transport. /// /// /// An I/O error occurred. /// /// /// The send command failed. /// /// /// A protocol exception occurred. /// public abstract void Send (FormatOptions options, MimeMessage message, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null); /// /// Asynchronously send the specified message. /// /// /// Asynchronously 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. /// /// An asynchronous task context. /// The formatting options. /// The message. /// The cancellation token. /// The progress reporting mechanism. /// /// is null. /// -or- /// is null. /// /// /// The has been disposed. /// /// /// The is not connected. /// /// /// Authentication is required before sending a message. /// /// /// A sender has not been specified. /// -or- /// No recipients have been specified. /// /// /// The operation has been canceled. /// /// /// Internationalized formatting was requested but is not supported by the transport. /// /// /// An I/O error occurred. /// /// /// The send command failed. /// /// /// A protocol exception occurred. /// public abstract Task SendAsync (FormatOptions options, MimeMessage message, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null); /// /// Send the specified message using the supplied sender and recipients. /// /// /// Sends the specified message using the supplied sender and recipients. /// /// The formatting options. /// The message. /// The mailbox address to use for sending the message. /// The mailbox addresses that should receive the message. /// The cancellation token. /// The progress reporting mechanism. /// /// is null. /// -or- /// is null. /// -or- /// is null. /// -or- /// is null. /// /// /// The has been disposed. /// /// /// The is not connected. /// /// /// Authentication is required before sending a message. /// /// /// A sender has not been specified. /// -or- /// No recipients have been specified. /// /// /// The operation has been canceled. /// /// /// Internationalized formatting was requested but is not supported by the transport. /// /// /// An I/O error occurred. /// /// /// The send command failed. /// /// /// A protocol exception occurred. /// public abstract void Send (FormatOptions options, MimeMessage message, MailboxAddress sender, IEnumerable recipients, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null); /// /// Asynchronously send the specified message using the supplied sender and recipients. /// /// /// Asynchronously sends the specified message using the supplied sender and recipients. /// /// An asynchronous task context. /// The formatting options. /// The message. /// The mailbox address to use for sending the message. /// The mailbox addresses that should receive the message. /// The cancellation token. /// The progress reporting mechanism. /// /// is null. /// -or- /// is null. /// -or- /// is null. /// -or- /// is null. /// /// /// The has been disposed. /// /// /// The is not connected. /// /// /// Authentication is required before sending a message. /// /// /// A sender has not been specified. /// -or- /// No recipients have been specified. /// /// /// The operation has been canceled. /// /// /// Internationalized formatting was requested but is not supported by the transport. /// /// /// An I/O error occurred. /// /// /// The send command failed. /// /// /// A protocol exception occurred. /// public abstract Task SendAsync (FormatOptions options, MimeMessage message, MailboxAddress sender, IEnumerable recipients, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null); /// /// Occurs when a message is successfully sent via the transport. /// /// /// The event will be emitted each time a message is successfully sent. /// public event EventHandler MessageSent; /// /// Raise the message sent event. /// /// /// Raises the message sent event. /// /// The message sent event args. protected virtual void OnMessageSent (MessageSentEventArgs e) { var handler = MessageSent; if (handler != null) handler (this, e); } } }