Exemplo n.º 1
0
        private static EmailMessageData CreateMessage(
            IEmailBuilder emailBuilder,
            bool copySender            = false,
            bool discloseSenderAddress = false)
        {
            var recipients = emailBuilder.GetRecipients();

            if (recipients == EmailRecipients.None)
            {
                // Optimization: no need to construct message body when no recipients.
                return(null);
            }

            if (emailBuilder.Sender == null)
            {
                throw new ArgumentException(
                          $"No sender defined for message of type '{emailBuilder.GetType()}'.",
                          nameof(emailBuilder.Sender));
            }

            return(new EmailMessageData(
                       emailBuilder.GetSubject(),
                       emailBuilder.GetBody(EmailFormat.PlainText),
                       emailBuilder.GetBody(EmailFormat.Html),
                       emailBuilder.Sender.Address,
                       to: recipients.To.Select(e => e.Address).ToList(),
                       cc: GenerateCC(
                           emailBuilder.Sender.Address,
                           recipients.CC.Select(e => e.Address).ToList(),
                           copySender,
                           discloseSenderAddress),
                       bcc: recipients.Bcc.Select(e => e.Address).ToList(),
                       replyTo: recipients.ReplyTo.Select(e => e.Address).ToList(),
                       messageTrackingId: Guid.NewGuid()));
        }
        private EmailMessageData CreateMessage(
            IEmailBuilder emailBuilder,
            bool copySender            = false,
            bool discloseSenderAddress = false)
        {
            var recipients = emailBuilder.GetRecipients();

            if (!recipients.To.Any())
            {
                _logger.LogInformation("Cannot create message to send as it has no recipients.");
                return(null);
            }

            if (emailBuilder.Sender == null)
            {
                throw new ArgumentException(
                          $"No sender defined for message of type '{emailBuilder.GetType()}'.",
                          nameof(emailBuilder.Sender));
            }

            return(new EmailMessageData(
                       emailBuilder.GetSubject(),
                       emailBuilder.GetBody(EmailFormat.PlainText),
                       emailBuilder.GetBody(EmailFormat.Html),
                       emailBuilder.Sender.Address,
                       to: recipients.To.Select(e => e.Address).ToList(),
                       cc: GenerateCC(
                           emailBuilder.Sender.Address,
                           recipients.CC.Select(e => e.Address).ToList(),
                           copySender,
                           discloseSenderAddress),
                       bcc: recipients.Bcc.Select(e => e.Address).ToList(),
                       replyTo: recipients.ReplyTo.Select(e => e.Address).ToList(),
                       messageTrackingId: Guid.NewGuid()));
        }
        public Task SendMessageAsync(IEmailBuilder emailBuilder, bool copySender = false, bool discloseSenderAddress = false)
        {
            _logger.LogInformation("Message Sending with Subject: {Subject}", emailBuilder.GetSubject());
            _logger.LogInformation("Body: {Body}", emailBuilder.GetBody(EmailFormat.PlainText));
            _logger.LogInformation("Sender: {Sender}", emailBuilder.Sender.Address);

            _logger.LogWarning("Empty messenger used. Sending a message is no-oping.");
            return(Task.CompletedTask);
        }
        private Task EnqueueMessageToSenderAsync(IEmailBuilder emailBuilder)
        {
            var originalRecipients = emailBuilder.GetRecipients();

            if (!originalRecipients.To.Any())
            {
                _logger.LogInformation("Cannot create message to sender as the original message has no recipients.");
                return(null);
            }

            if (emailBuilder.Sender == null)
            {
                throw new ArgumentException(
                          $"No sender defined for message of type '{emailBuilder.GetType()}'.",
                          nameof(emailBuilder.Sender));
            }

            var plainTextBody = string.Format(
                CultureInfo.CurrentCulture,
                "You sent the following message via {0}: {1}{1}{2}",
                _configuration.GalleryOwner.DisplayName,
                Environment.NewLine,
                emailBuilder.GetBody(EmailFormat.PlainText));

            var htmlBody = string.Format(
                CultureInfo.CurrentCulture,
                "You sent the following message via {0}: {1}{1}{2}",
                _configuration.GalleryOwner.DisplayName,
                Environment.NewLine,
                emailBuilder.GetBody(EmailFormat.Html));

            // We do not CC or BCC anyone as we do not want to disclose the sender address
            // when sending a separate message (otherwise we'd just have CC-ed the sender).
            var messageToSender = new EmailMessageData(
                emailBuilder.GetSubject() + " [Sender Copy]",
                plainTextBody,
                htmlBody,
                sender: _configuration.GalleryOwner.Address,
                to: originalRecipients.ReplyTo.Select(e => e.Address).ToList(),
                cc: null,
                bcc: null,
                replyTo: originalRecipients.ReplyTo.Select(e => e.Address).ToList(),
                messageTrackingId: Guid.NewGuid());

            return(_emailMessageEnqueuer.SendEmailMessageAsync(messageToSender));
        }
Exemplo n.º 5
0
        protected static MailMessage CreateMailMessage(IEmailBuilder emailBuilder)
        {
            if (emailBuilder == null)
            {
                throw new ArgumentNullException(nameof(emailBuilder));
            }

            var mailMessage = new MailMessage();

            mailMessage.From    = emailBuilder.Sender;
            mailMessage.Subject = emailBuilder.GetSubject();
            mailMessage.Body    = emailBuilder.GetBody(EmailFormat.Markdown);

            var recipients = emailBuilder.GetRecipients();

            foreach (var toAddress in recipients.To)
            {
                mailMessage.To.Add(toAddress);
            }

            foreach (var ccAddress in recipients.CC)
            {
                mailMessage.CC.Add(ccAddress);
            }

            foreach (var bccAddress in recipients.Bcc)
            {
                mailMessage.Bcc.Add(bccAddress);
            }

            foreach (var replyToAddress in recipients.ReplyTo)
            {
                mailMessage.ReplyToList.Add(replyToAddress);
            }

            return(mailMessage);
        }
 public string GetSubject()
 {
     // run through a replacer
     return(_parentEmailBuilder.GetSubject().Replace(USERNAME_PLACEHOLDER, _username));
 }