Пример #1
0
        public override Task SendEmail(MailRequest request, CancellationToken cancellationToken)
        {
            ThrowIfNotConfigured();

            jobClient.Enqueue <SendSingleQueuedEmailJob>(x => x.Execute(request, CancellationToken.None));
            return(Task.CompletedTask);
        }
Пример #2
0
        public virtual async Task SendEmail(MailRequest request, CancellationToken cancellationToken)
        {
            ThrowIfNotConfigured();

            var sender = MailboxAddress.Parse(fromAddress);

            sender.Name = senderName;

            var builder = new BodyBuilder {
                HtmlBody = request.HtmlBody, TextBody = request.PlainTextBody
            };

            if (request.Attachments != null)
            {
                foreach (var attachment in request.Attachments)
                {
                    builder.Attachments.Add(attachment.Filename, Encoding.UTF8.GetBytes(attachment.Content),
                                            ContentType.Parse(attachment.MimeType));
                }
            }

            var email = new MimeMessage
            {
                Sender  = sender,
                Subject = request.Subject,
                Body    = builder.ToMessageBody(),
            };

            if (!string.IsNullOrEmpty(request.Bcc))
            {
                email.Bcc.Add(MailboxAddress.Parse(request.Bcc));
            }

            if (!string.IsNullOrEmpty(request.Cc))
            {
                email.Cc.Add(MailboxAddress.Parse(request.Cc));
            }

            if (!string.IsNullOrEmpty(request.ReplyTo))
            {
                email.ReplyTo.Add(MailboxAddress.Parse(request.ReplyTo));
            }

            email.From.Add(sender);

            email.To.Add(MailboxAddress.Parse(request.Recipient));

            // TODO: batching of email send requests (or perhaps queue sender jobs would be better for this)

            using var smtp = new SmtpClient();

            SecureSocketOptions connectMode = SecureSocketOptions.StartTls;

            if (!requireTls)
            {
                connectMode = SecureSocketOptions.StartTlsWhenAvailable;
            }

            // When using ssl port, switch to SSL mode
            if (port == 465)
            {
                connectMode = SecureSocketOptions.SslOnConnect;
            }

            await smtp.ConnectAsync(host, port, connectMode, cancellationToken);

            // Only authenticate if we have a password set
            if (!string.IsNullOrEmpty(emailPassword))
            {
                await smtp.AuthenticateAsync(fromAddress, emailPassword, cancellationToken);
            }

            logger.LogInformation("Sending email to {Recipient}", request.Recipient);
            await smtp.SendAsync(email, cancellationToken);

            // If this is canceled here, then it might be possible that a single email is sent twice, but should be
            // very rare
            await smtp.DisconnectAsync(true, cancellationToken);
        }