public void Send(Email email) { Required.NotNull(email, "Email"); Required.NotNullOrEmpty(email.To, string.Empty, "To"); Required.NotNullOrEmpty(email.From, string.Empty, "From"); Required.NotNullOrEmpty(email.Subject, string.Empty, "Subject"); Required.NotNullOrEmpty(email.Body, string.Empty, "Body"); email.CreateDate = DateTime.Now; email.Attempts = 0; _servicesRepository.Add(email); _servicesRepository.Save(); }
public void Send(Email email) { Required.NotNull(email, "Email"); Required.NotNullOrEmpty(email.To, string.Empty, "To"); Required.NotNullOrEmpty(email.From, string.Empty, "From"); Required.NotNullOrEmpty(email.Subject, string.Empty, "Subject"); Required.NotNullOrEmpty(email.Body, string.Empty, "Body"); var mailMessage = new MailMessage(); var smtpClient = new SmtpClient(); try { if (!string.IsNullOrEmpty(email.From)) { mailMessage.From = new MailAddress(email.From); } if (!string.IsNullOrEmpty(email.To)) { email.To.Split(';', ',').ForEach(address => { if (!string.IsNullOrEmpty(address)) { mailMessage.To.Add(address); } }); } if (!string.IsNullOrEmpty(email.Cc)) { email.Cc.Split(';', ',').ForEach(address => { if (!string.IsNullOrEmpty(address)) { mailMessage.CC.Add(address); } }); } if (!string.IsNullOrEmpty(email.Bcc)) { email.Bcc.Split(';', ',').ForEach(address => { if (!string.IsNullOrEmpty(address)) { mailMessage.Bcc.Add(address); } }); } var re = new Regex(REGEX_HTML, RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture); mailMessage.Subject = email.Subject; mailMessage.Body = email.Body; mailMessage.IsBodyHtml = re.IsMatch(email.Body); smtpClient.Send(mailMessage); Log(email, "Email successfully sent."); } catch (Exception ex) { var message = "Email send failed."; if (ex is SmtpFailedRecipientException) { message += string.Format(" Recipient: {0}.", (ex as SmtpFailedRecipientException).FailedRecipient); } Log(email, message, ex); } }