Beispiel #1
0
        private async Task SendEmail(ChapterEmailProvider provider, MimeMessage message)
        {
            if (message.To.Count == 0)
            {
                await _loggingService.LogDebug("Not sending email, no recipients set");

                return;
            }

            await _loggingService.LogDebug($"Sending email to {string.Join(", ", message.To)}");

            try
            {
                using SmtpClient client = new SmtpClient
                      {
                          ServerCertificateValidationCallback = (s, c, h, e) => true
                      };
                await client.ConnectAsync(provider.SmtpServer, provider.SmtpPort, SecureSocketOptions.StartTlsWhenAvailable);

                client.Authenticate(provider.SmtpLogin, provider.SmtpPassword);

                await client.SendAsync(message);

                await client.DisconnectAsync(true);

                foreach (InternetAddress to in message.To.Union(message.Cc).Union(message.Bcc))
                {
                    SentEmail sentEmail = new SentEmail(provider.Id, DateTime.UtcNow, to.ToString(), message.Subject);
                    await _emailRepository.AddSentEmail(sentEmail);
                }

                await _loggingService.LogDebug($"Email sent to {string.Join(", ", message.To)}");
            }
            catch (Exception ex)
            {
                await _loggingService.LogError(ex, "Error sending email");

                throw new OdkServiceException("Error sending email");
            }
        }