public async Task <bool> Send(IEmailMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (message.SendableDate > LocalTime.Now) { Log.Info($"Skipping Send() command for IEmailMessage ({message.GetId()}). SendableDate is in the future."); if (message.IsNew) { await Database.Save(message); } return(false); } using (var mail = await MessageCreator.Create(message)) { if (mail.To.None() && mail.CC.None() && mail.Bcc.None()) { Log.Info($"Mail message {message.GetId()} will not be sent as there is no effective recipient."); return(false); } try { await Sending.Raise(new EmailSendingEventArgs(message, mail)); await Dispatcher.Dispatch(mail, message); if (!message.IsNew) { await Database.Delete(message); } await Sent.Raise(new EmailSendingEventArgs(message, mail)); return(true); } catch (Exception ex) { await SendError.Raise(new EmailSendingEventArgs(message, mail) { Error = ex }); await RecordRetry(message); Log.Error(ex, $"Error in sending an email for this EmailQueueItem of '{message.GetId()}' because : " + Environment.NewLine + ex.ToFullMessage()); return(false); } } }
/// <summary> /// Will try to send the specified email and returns true for successful sending. /// </summary> public static async Task <bool> Send(IEmailMessage mailItem) { if (mailItem == null) { throw new ArgumentNullException(nameof(mailItem)); } if (mailItem.Retries >= MaximumRetries) { return(false); } MailMessage mail = null; try { using (mail = await CreateMailMessage(mailItem)) { if (mail == null) { return(false); } return(await EmailDispatcher(mailItem, mail)); } } catch (Exception ex) { await SendError.Raise(new EmailSendingEventArgs(mailItem, mail) { Error = ex }); await mailItem.RecordRetry(); Log.Error($"Error in sending an email for this EmailQueueItem of '{mailItem.GetId()}'", ex); return(false); } }
/// <summary> /// Creates an SMTP mail message for a specified mail item. /// </summary> static async Task <MailMessage> CreateMailMessage(IEmailMessage mailItem) { if (mailItem.SendableDate > LocalTime.Now) { return(null); // Not due yet } var mail = new MailMessage { Subject = mailItem.Subject.Or("[NO SUBJECT]").Remove("\r", "\n") }; mailItem.GetEffectiveToAddresses().Do(x => mail.To.Add(x)); mailItem.GetEffectiveCcAddresses().Do(x => mail.CC.Add(x)); mailItem.GetEffectiveBccAddresses().Do(x => mail.Bcc.Add(x)); if (mail.To.None() && mail.CC.None() && mail.Bcc.None()) { Debug.WriteLine($"Mail message {mailItem.GetId()} will not be sent as there is no effective recipient."); return(null); } mail.AlternateViews.AddRange(mailItem.GetEffectiveBodyViews()); mail.From = new MailAddress(mailItem.GetEffectiveFromAddress(), mailItem.GetEffectiveFromName()); mail.ReplyToList.Add(new MailAddress(mailItem.GetEffectiveReplyToAddress(), mailItem.GetEffectiveReplyToName())); mail.Attachments.AddRange(await mailItem.GetAttachments()); return(mail); }
/// <summary> /// Will try to send the specified email and returns true for successful sending. /// </summary> public static async Task <bool> Send(IEmailMessage mailItem) { if (mailItem == null) { throw new ArgumentNullException(nameof(mailItem)); } if (mailItem.Retries >= MaximumRetries) { return(false); } MailMessage mail = null; try { using (mail = await CreateMailMessage(mailItem)) { if (mail == null) { return(false); } await Sending.Raise(new EmailSendingEventArgs(mailItem, mail)); var dispatcher = Context.Current.GetOptionalService <IEmailDispatcher>() ?? new DefaultEmailDispatcher(); var result = await dispatcher.Dispatch(mailItem, mail); await Sent.Raise(new EmailSendingEventArgs(mailItem, mail)); return(result); } } catch (Exception ex) { await SendError.Raise(new EmailSendingEventArgs(mailItem, mail) { Error = ex }); await mailItem.RecordRetry(); Log.For(typeof(EmailService)) .Error(ex, $"Error in sending an email for this EmailQueueItem of '{mailItem.GetId()}'"); return(false); } }
public async Task <MailMessage> Create(IEmailMessage message) { var result = new MailMessage { Subject = message.Subject.Or("[NO SUBJECT]").Remove("\r", "\n"), Body = message.Body, From = CreateFrom(message) }; result.Headers .Add(Constants.EMAIL_MESSAGE_ID_HEADER_KEY, message.GetId().ToString()); GetEffectiveRecipients(message.To).Do(x => result.To.Add(x)); GetEffectiveRecipients(message.Cc + Config.AutoAddCc.WithPrefix(",")).Do(x => result.CC.Add(x)); GetEffectiveRecipients(message.Bcc + Config.AutoAddBcc.WithPrefix(",")).Do(x => result.Bcc.Add(x)); result.ReplyToList.Add(CreateReplyTo(message)); result.AlternateViews.AddRange(GetBodyViews(message)); result.Attachments.AddRange(await AttachmentSerializer.Extract(message)); return(result); }