Example #1
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));
        }
Example #2
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));
        }