public void SendEmail(Email email) { Log.Info("Sending email from '{0}' to '{1}' with subject '{2}'.", email.From, email.To, email.Subject); try { TrySendEmail(email); email.IsSent = true; email.SendTime = DateTime.UtcNow; Log.Info("Email from '{0}' to '{1}' with subject '{2}' was sent successfully with attempt #{3}", email.From, email.To, email.Subject, email.SendAttempt); } catch (SmtpException ex) { Log.Error("Email sending failed. Exception {0}", ex.ToString()); email.IsSent = false; } catch (InvalidOperationException ex) { Log.Error("Email sending failed. Exception {0}", ex.ToString()); email.IsSent = false; } finally { email.SendAttempt++; email.IsForceSend = false; _emailRepository.Save(email); } }
private static void TrySendEmail(Email email) { var mailToSend = new MailMessage(); mailToSend.To.Add(email.To); mailToSend.From = new MailAddress(email.From); mailToSend.Subject = email.Subject; mailToSend.Body = email.Body; mailToSend.IsBodyHtml = email.IsHtml; var smtp = new SmtpClient(); smtp.Send(mailToSend); }
private bool ShouldBeSend(Email email) { if (email.IsForceSend) { return true; } if (email.SendAttempt >= _resendingIntervalsInMinutes.Length) { return false; } return email.SubmitTime.AddMinutes(_resendingIntervalsInMinutes[email.SendAttempt]) < DateTime.UtcNow; }