Ejemplo n.º 1
0
        private MailAddress GetFromMailAddress(SendEmailArgs args)
        {
            string emailFrom = string.IsNullOrWhiteSpace(args?.FromEmailOverride)
                ? Options.FromEmail
                : args.FromEmailOverride;

            string emailFromDisplayName = string.IsNullOrWhiteSpace(args?.FromDisplayNameOverride)
                ? Options.FromDisplayName
                : args.FromDisplayNameOverride;

            return(new MailAddress(emailFrom, emailFromDisplayName));
        }
Ejemplo n.º 2
0
        public void SendEmail(
            IEnumerable <EmailAddressInfo> emailsTo,
            string subject,
            string bodyHtml,
            SendEmailArgs args = null)
        {
            if (emailsTo == null || !emailsTo.Any())
            {
                throw new ArgumentNullException(nameof(emailsTo));
            }

            ValidateWhiteList(emailsTo, args.EmailsCc, args.EmailsBcc);

            using (var letter = new MailMessage())
            {
                letter.Subject    = subject;
                letter.IsBodyHtml = true;

                var htmlView = AlternateView.CreateAlternateViewFromString(bodyHtml, null, "text/html");
                letter.AlternateViews.Add(htmlView);

                AddEmailsAddresses(letter.To, emailsTo);
                AddEmailsAddresses(letter.CC, args.EmailsCc);
                AddEmailsAddresses(letter.Bcc, args.EmailsBcc);
                AddEmailsAddresses(letter.ReplyToList, args.EmailsReplyTo);
                AddHeaders(letter.Headers, args.EmailHeaders);
                AddAttachments(letter.Attachments, args.Attachments);
                AddLinkedResources(htmlView.LinkedResources, args.LinkedResources);

                using (var smtp = new SmtpClient(Options.Host, Options.Port))
                {
                    smtp.Credentials = new NetworkCredential(Options.UserName, Options.Password);
                    letter.From      = GetFromMailAddress(args);
                    smtp.Send(letter);
                }
            }
        }