public MailMessage Build()
        {
            MailMessage mailMessage = new MailMessage();

            if (From == null || string.IsNullOrWhiteSpace(From.Address))
            {
                throw new ArgumentException(nameof(From));
            }
            else
            {
                mailMessage.From = From;
            }

            if (To != null && To.Any())
            {
                mailMessage.To.AddRange(To);
            }

            if (CC != null && CC.Any())
            {
                mailMessage.CC.AddRange(CC);
            }

            if (Bcc != null && Bcc.Any())
            {
                mailMessage.Bcc.AddRange(Bcc);
            }

            if (!string.IsNullOrWhiteSpace(Subject))
            {
                mailMessage.Subject = Subject;
            }

            if (!string.IsNullOrWhiteSpace(Body))
            {
                mailMessage.Body       = Body;
                mailMessage.IsBodyHtml = IsBodyHtml;
            }

            if (Attachments != null && Attachments.Any())
            {
                mailMessage.Attachments.AddRange(Attachments);
            }

            return(mailMessage);
        }
Beispiel #2
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var validationResults = new List <ValidationResult>();

            foreach (string email in To)
            {
                ValidateEmail(validationResults, email, nameof(To), "To");
            }
            foreach (string email in CC)
            {
                ValidateEmail(validationResults, email, nameof(CC), "CC");
            }
            foreach (string email in Bcc)
            {
                ValidateEmail(validationResults, email, nameof(Bcc), "BCC");
            }
            if (!To.Any() && !CC.Any() && !Bcc.Any())
            {
                validationResults.Add(new ValidationResult("This email doesn't define a recipient.", new[] { nameof(To) }));
            }
            return(validationResults);
        }