示例#1
0
        /// <summary>
        /// Sends an email message asynchronously.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="sessionId">The session identifier.</param>
        /// <returns>A task that represents the asynchronous of send email operation</returns>
        public Task SendMailAsync(MailMessage message, string sessionId = null)
        {
            var state = new SmtpSessionState
            {
                AuthMode        = Credentials == null ? string.Empty : SmtpDefinitions.SmtpAuthMethods.Login,
                ClientHostname  = ClientHostname,
                IsChannelSecure = EnableSsl,
                SenderAddress   = message.From.Address
            };

            if (Credentials != null)
            {
                state.Username = Credentials.UserName;
                state.Password = Credentials.Password;
            }

            foreach (var recipient in message.To)
            {
                state.Recipients.Add(recipient.Address);
            }

            state.DataBuffer.AddRange(message.ToMimeMessage().ToArray());

            return(SendMailAsync(state, sessionId));
        }
示例#2
0
 /// <summary>
 /// Sends an email message using a session state object.
 /// Credentials, Enable SSL and Client Hostname are NOT taken from the state object but
 /// rather from the properties of this class.
 /// </summary>
 /// <param name="sessionState">The state.</param>
 /// <param name="sessionId">The session identifier.</param>
 /// <param name="ct">The cancellation token.</param>
 /// <returns>A task that represents the asynchronous of send email operation</returns>
 public async Task SendMailAsync(
     SmtpSessionState sessionState,
     string sessionId     = null,
     CancellationToken ct = default(CancellationToken))
 {
     $"Sending new email from {sessionState.SenderAddress} to {string.Join(";", sessionState.Recipients)}".Info(
         typeof(SmtpClient));
     await SendMailAsync(new[] { sessionState }, sessionId, ct);
 }
示例#3
0
        /// <summary>
        /// Sends an email message using a session state object.
        /// Credentials, Enable SSL and Client Hostname are NOT taken from the state object but
        /// rather from the properties of this class.
        /// </summary>
        /// <param name="sessionState">The state.</param>
        /// <param name="sessionId">The session identifier.</param>
        /// <param name="ct">The cancellation token.</param>
        /// <returns>
        /// A task that represents the asynchronous of send email operation.
        /// </returns>
        /// <exception cref="ArgumentNullException">sessionState.</exception>
        public Task SendMailAsync(
            SmtpSessionState sessionState,
            string sessionId     = null,
            CancellationToken ct = default)
        {
            if (sessionState == null)
            {
                throw new ArgumentNullException(nameof(sessionState));
            }

            return(SendMailAsync(new[] { sessionState }, sessionId, ct));
        }
示例#4
0
        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>A clone</returns>
        public virtual SmtpSessionState Clone()
        {
            // TODO: Use a binary formatter or something similar when it comes out in .net core/standard
            // Binary Formatter is not good (https://github.com/force-net/DeepCloner)
            var result = new SmtpSessionState();

            this.CopyPropertiesTo(result);

            result.Recipients.AddRange(Recipients);
            result.DataBuffer.AddRange(DataBuffer);

            return(result);
        }
示例#5
0
        /// <summary>
        /// Sends an email message using a session state object.
        /// Credentials, Enable SSL and Client Hostname are NOT taken from the state object but
        /// rather from the properties of this class.
        /// </summary>
        /// <param name="sessionState">The state.</param>
        /// <param name="sessionId">The session identifier.</param>
        /// <param name="ct">The cancellation token.</param>
        /// <returns>
        /// A task that represents the asynchronous of send email operation
        /// </returns>
        /// <exception cref="ArgumentNullException">sessionState</exception>
        public Task SendMailAsync(
            SmtpSessionState sessionState,
            string sessionId     = null,
            CancellationToken ct = default)
        {
            if (sessionState == null)
            {
                throw new ArgumentNullException(nameof(sessionState));
            }

            $"Sending new email from {sessionState.SenderAddress} to {string.Join(";", sessionState.Recipients)}".Info(
                typeof(SmtpClient));
            return(SendMailAsync(new[] { sessionState }, sessionId, ct));
        }