Esempio n. 1
0
        private async Task <IOutcome> SendOverMultipleSmtpServersAsync(EmailMessage message)
        {
            var errors = new List <string>();
            var smtpServerConfigurations = _mailSettingsConfig.Smtp.Servers;
            var wasSentSuccessfully      = false;

            foreach (var smtpServerConfiguration in smtpServerConfigurations)
            {
                try
                {
                    if (wasSentSuccessfully)
                    {
                        continue;
                    }

                    var createRequest = new MailMessageCreateRequest()
                    {
                        EmailMessage           = message,
                        SmtpServerConfig       = smtpServerConfiguration,
                        IsReadSenderFromConfig = true,
                        RequiredRecipients     = _mailSettingsConfig.Smtp.RequiredRecipients,
                    };
                    var mailMessage = await _mailMessageFactory.CreateAsync(createRequest);

                    using (var smtpClient = _smtpClientFactory.Create(smtpServerConfiguration))
                    {
                        await smtpClient.SendAsync(mailMessage);

                        wasSentSuccessfully = true;
                    }
                }
                catch (Exception ex)
                {
                    wasSentSuccessfully = false;

                    var error = ex.Message;
                    if (ex.InnerException != null)
                    {
                        var innerEx = ex.InnerException;
                        while (innerEx.InnerException != null)
                        {
                            innerEx = innerEx.InnerException;
                        }

                        error = innerEx.Message;
                    }

                    errors.Add($"SMTP {smtpServerConfiguration.Host} throw an error: {error}");
                }
            }

            if (wasSentSuccessfully)
            {
                return(Outcomes.Success());
            }

            return(Outcomes.Failure().WithMessagesFrom(errors.Distinct()));
        }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogTrace("Starting send email...");

            var emailMessage = await _emailMessageFactory.CreateAsync(null);

            var sendResult = await _notificator.NotifyAsync(emailMessage);

            _logger.LogInformation($"Email sent result:\n\n{sendResult}");
        }
Esempio n. 3
0
        public async Task SendAsync(IMessage message)
        {
            IPreparedMessage preparedMessage = await _messageFactory.CreateAsync(message);

            _pendingMessageContainer.Add(preparedMessage);
        }