Exemple #1
0
        /// <summary>
        /// Sends an email message asynchronously.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="sessionId">The session identifier.</param>
        /// <param name="callback">The callback.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// A task that represents the asynchronous of send email operation.
        /// </returns>
        /// <exception cref="ArgumentNullException">message.</exception>
        public Task SendMailAsync(
            MailMessage message,
            string?sessionId = null,
            RemoteCertificateValidationCallback?callback = null,
            CancellationToken cancellationToken          = default)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            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, callback, cancellationToken));
        }
Exemple #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="callback">The callback.</param>
        /// <param name="cancellationToken">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,
            RemoteCertificateValidationCallback?callback = null,
            CancellationToken cancellationToken          = default)
        {
            if (sessionState == null)
            {
                throw new ArgumentNullException(nameof(sessionState));
            }

            return(SendMailAsync(new[] { sessionState }, sessionId, callback, cancellationToken));
        }