private void SendMessageFor(Email message) { using (var email = _emailGenerator()) { SetupEmailFromMessage(message, email); _config.Logger.LogInfo(string.Join(string.Empty, "Attempting to send mail to '", email.To.FirstOrDefault())); try { email.Send(); message.Sent = true; _config.Logger.LogInfo(" => message sent"); } catch (Exception ex) { message.LastError = ex.Message; message.SendAttempts++; var backoffBy = _config.BackoffIntervalInMinutes * message.SendAttempts; if (message.SendAttempts > 1) { backoffBy *= _config.BackoffMultiplier; } message.SendAt = DateTime.Now.AddMinutes(backoffBy); message.Sent = false; _config.Logger.LogInfo("** Message not sent: " + ex.Message); } _context.SaveChanges(); } }
private static void SetupEmailFromMessage(Email message, IEmail email) { AddAllRecipients(message, email); email.From = message.Sender; email.Subject = message.Subject; email.Body = message.Body; AddAttachments(message, email); }
private static void AddAttachments(Email message, IEmail email) { foreach (var attachment in message.EmailAttachments.Where(a => a.Enabled)) { if (string.IsNullOrEmpty(attachment.ContentID)) { email.AddAttachment(attachment.Name, attachment.Data, attachment.MIMEType); } else { email.AddAttachment(attachment.Name, attachment.Data, attachment.MIMEType, attachment.ContentID); } } }
private static void AddAllRecipients(Email message, IEmail email) { foreach (var recipient in message.EmailRecipients.Where(r => r.Enabled)) { if (recipient.IsPrimaryRecipient) { email.AddRecipient(recipient.Recipient); } if (recipient.IsCC) { email.AddCC(recipient.Recipient); } if (recipient.IsBCC) { email.AddBCC(recipient.Recipient); } } }