private async Task SendEmailAsync(IMailTransport transport, IEnumerable <MimeMessage> messages)
        {
            try
            {
                foreach (var message in messages.ToList())
                {
                    transport.ServerCertificateValidationCallback = (s, c, h, e) => true;

                    await transport.ConnectAsync(
                        Settings.Default.Host,
                        Settings.Default.Port,
                        SecureSocketOptions.Auto).ConfigureAwait(false);

                    transport.AuthenticationMechanisms.Remove("XOAUTH2");

                    await transport.AuthenticateAsync(
                        Settings.Default.Login,
                        Settings.Default.Password).ConfigureAwait(false);

                    await transport.SendAsync(message).ConfigureAwait(false);

                    await transport.DisconnectAsync(true).ConfigureAwait(false);
                }
            }
            catch (Exception exception)
            {
                _logger.Error(exception.Message);
            }
        }
Beispiel #2
0
 public EmailSender(
     IConfiguration configuration,
     IMailTransport mailTransport
     )
 {
     Configuration = configuration;
     MailTransport = mailTransport;
 }
 /// <summary>Configures the mail client.</summary>
 /// <param name="mailClient">The mailClient to configure.</param>
 /// <returns>The configured mailClient.</returns>
 protected override IMailTransport ConfigureMailClient(IMailTransport mailClient)
 {
     mailClient.Connected     += (sender, args) => Logger?.LogDebug($"SMTP-Client connected. Host: {args.Host}");
     mailClient.Authenticated += (sender, args) => Logger?.LogDebug("SMTP-Client authenticated.");
     mailClient.MessageSent   += (sender, args) => Logger?.LogDebug("SMTP-Client sent message.");
     mailClient.Disconnected  += (sender, args) => Logger?.LogDebug("SMTP-Client disconnected.");
     return(mailClient);
 }
        public MailKitEmailService(IMailTransport client, SmtpSettings settings, ILogger <MailKitEmailService> logger)
        {
            _client   = client ?? throw new ArgumentNullException(nameof(client));
            _settings = settings ?? throw new ArgumentNullException(nameof(client));
            _logger   = logger ?? throw new ArgumentNullException(nameof(logger));

            if (settings.AllowInvalidCertificate.GetValueOrDefault())
            {
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;
            }
        }
        /// <summary>
        /// Asynchronously sends the specified messages.
        /// </summary>
        /// <param name="messages">The collection of messages to send.</param>
        /// <param name="cancellationToken">The token used to cancel an ongoing async operation.</param>
        /// <returns></returns>
        public virtual async Task SendAsync(IEnumerable <MimeMessage> messages, CancellationToken cancellationToken = default)
        {
            IMailTransport client = null;

            try
            {
                client = await CreateOutgoingMailClientAsync(cancellationToken);

                foreach (var message in messages)
                {
                    try
                    {
                        await client.SendAsync(message, cancellationToken);
                        await OnSuccessAsync(message);
                    }
                    catch (Exception ex)
                    {
                        if (Error != null)
                        {
                            await Error(new SendEventArgs(new MimeMessage[] { message }, ex));
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (Error != null)
                {
                    await Error(new SendEventArgs(messages, ex));
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                if (client != null)
                {
                    client.Disconnect(true);

                    if (client is IDisposable obj)
                    {
                        obj.Dispose();
                    }
                }
            }
        }
        private async Task ConnectAsync(IMailTransport client)
        {
            await client.ConnectAsync(
                host : options.Smtp.Host,
                port : options.Smtp.Port,
                useSsl : false);

            //Note: only needed if the SMTP server requires authentication
            if (!string.IsNullOrWhiteSpace(options.Smtp.Username) &&
                !string.IsNullOrWhiteSpace(options.Smtp.Password))
            {
                await client.AuthenticateAsync(
                    userName : options.Smtp.Username,
                    password : options.Smtp.Password);
            }
        }
 public TestMailKitSmtpMailService(MailServiceOptions options, IMailTransport mailClient) : base(options)
 {
     _mailClient = mailClient;
 }
 public EmailController(IMailTransport mailTransport, SmtpSettings settings)
 {
     _mailTransport = mailTransport;
     _settings      = settings;
 }
 public TestLoggingMailService(MailServiceOptions options, ILogger <LoggingMailService> logger,
                               IMailTransport mailClient) : base(options, logger)
 {
     _mailClient = mailClient;
 }
Beispiel #10
0
 public EmailSenderImpl(IMailTransport smtpClient, ReportFormatter reportFormatter, Settings settings)
 {
     this.smtpClient      = smtpClient;
     this.reportFormatter = reportFormatter;
     this.settings        = settings;
 }
 private static Task DisconnectAsync(IMailTransport client)
 {
     return(client.DisconnectAsync(true));
 }
 public MailingService(IMailTransport client, MailingServiceOptions options)
 {
     this.client  = client ?? throw new ArgumentNullException(nameof(client));
     this.options = options ?? throw new ArgumentNullException(nameof(options));
 }
 public EmailControllerTests()
 {
     _mailTransportMock = Substitute.For <IMailTransport>();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SMTPMailTransportProvider"/> class.
 /// </summary>
 /// <param name="configuration">Configuration which is going to be used for SMTP Mail</param>
 public SMTPMailTransportProvider(IOptions <EmailAppSettings> emailAppSettings, IMailTransport mailTransport)
 {
     this.emailAppSettings = emailAppSettings;
     this.mailTransport    = mailTransport;
 }
Beispiel #15
0
 /// <summary>Configures the mail client.</summary>
 /// <param name="mailClient">The mailClient to configure.</param>
 /// <returns>The configured mailClient.</returns>
 protected virtual IMailTransport ConfigureMailClient(IMailTransport mailClient)
 {
     mailClient.Timeout = Options.TimeOut;
     return(mailClient);
 }
Beispiel #16
0
 /// <summary>
 ///     Установка таймаута.
 /// </summary>
 /// <param name="transport"> Транспортный протокол. </param>
 /// <param name="timeout"> Таймаут. </param>
 private static void AddTimeout(IMailTransport transport, int?timeout)
 {
     transport.Timeout = timeout ?? transport.Timeout;
 }