Exemplo n.º 1
0
        void ValidateMail(MailBase mail)
        {
            if (mail.To == null || !mail.To.Any())
            {
                throw new ArgumentNullException("To");
            }

            if (string.IsNullOrWhiteSpace(mail.Sender))
            {
                throw new ArgumentNullException("Sender");
            }

            if (string.IsNullOrWhiteSpace(mail.Subject))
            {
                throw new ArgumentNullException("Subject");
            }

            if (!mail.BodyIsFile && string.IsNullOrWhiteSpace(mail.Body))
            {
                throw new ArgumentNullException("Body");
            }

            if (mail.BodyIsFile && string.IsNullOrWhiteSpace(mail.BodyPath))
            {
                throw new ArgumentNullException("BodyPath");
            }
        }
Exemplo n.º 2
0
        private async Task <MailMessage> BuildMailMessage(MailBase mail, StringDictionary replacements = null)
        {
            ValidateMail(mail);

            var sender = new MailAddress(mail.Sender, mail.SenderDisplayName);

            var mailMessage = new MailMessage()
            {
                Subject    = mail.Subject,
                IsBodyHtml = mail.IsBodyHtml,
                From       = sender,
            };

            var mailBody = !mail.BodyIsFile ? mail.Body : await GetEmailBodyTemplate(mail.BodyPath);

            if (replacements != null)
            {
                mailBody = Replace(mailBody, replacements, false);
            }

            mailMessage.Body = mailBody;

            if (mail.Attachments != null && mail.Attachments.Any())
            {
                //for (int i = 0; i < mail.Attachments.Count-1; i++) {
                //    mailMessage.Attachments.Add(mail.Attachments.ElementAt(i));
                //}
                foreach (var attachment in mail.Attachments)
                {
                    mailMessage.Attachments.Add(attachment);
                }
            }

            foreach (var to in mail.To)
            {
                mailMessage.To.Add(to);
            }

            if (mail.Bcc != null && mail.Bcc.Any())
            {
                foreach (var bcc in mail.Bcc)
                {
                    mailMessage.Bcc.Add(bcc);
                }
            }

            if (mail.CC != null && mail.CC.Any())
            {
                foreach (var cc in mail.CC)
                {
                    mailMessage.CC.Add(cc);
                }
            }

            return(mailMessage);
        }
Exemplo n.º 3
0
        protected virtual async Task SendMailAsync(MailBase mail, StringDictionary replacements)
        {
            var message = BuildMailMessage(mail, replacements).Result;

            try {
                using (var _smtpClient = GetSmtpClient()) {
                    await _smtpClient.SendMailAsync(message);
                }
            }
            catch (Exception e) {
                _logger.LogError(e.Message, e);
                throw;
            }
        }
Exemplo n.º 4
0
 async Task IMailService.SendMailb(MailBase mail, string subject, string bodl)
 {
     await SendMailAsync(mail, null);
 }
Exemplo n.º 5
0
 async Task IMailService.SendMailAsync(MailBase mail, StringDictionary replacements)
 {
     await SendMailAsync(mail, replacements);
 }
Exemplo n.º 6
0
 async Task IMailService.SendMailAsync(MailBase mail)
 {
     await SendMailAsync(mail, null);
 }
Exemplo n.º 7
0
 void IMailService.SendMail(MailBase mail, StringDictionary replacements)
 {
     SendMail(mail, replacements);
 }
Exemplo n.º 8
0
 void IMailService.SendMail(MailBase mail)
 {
     SendMail(mail, null);
 }