public static MailAddress GetReplyTo(this IEmailQueueItem mailItem) { var result = mailItem.GetSender(); var asCustomReplyTo = mailItem as ICustomReplyToEmailQueueItem; if (asCustomReplyTo == null) { return(result); } return(new MailAddress(asCustomReplyTo.ReplyToAddress.Or(result.Address), asCustomReplyTo.ReplyToName.Or(result.DisplayName))); }
/// <summary> /// Creates an SMTP mail message for a specified mail item. /// </summary> static MailMessage CreateMailMessage(IEmailQueueItem mailItem) { // Make sure it's due: if (mailItem.Date > LocalTime.Now) { return(null); } var mail = new MailMessage { Subject = mailItem.Subject.Or("[NO SUBJECT]").Remove("\r", "\n") }; #region Set Body if (mailItem.Html) { var htmlView = AlternateView.CreateAlternateViewFromString(mailItem.Body, new ContentType("text/html; charset=UTF-8")); //Add Linked Resources htmlView.LinkedResources.AddRange <LinkedResource>(mailItem.GetLinkedResources()); mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(mailItem.Body.RemoveHtmlTags(), new ContentType("text/plain; charset=UTF-8"))); mail.AlternateViews.Add(htmlView); } else { mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(mailItem.Body.RemoveHtmlTags(), new ContentType("text/plain; charset=UTF-8"))); } if (mailItem.VCalendarView.HasValue()) { var calendarType = new ContentType("text/calendar"); calendarType.Parameters.Add("method", "REQUEST"); calendarType.Parameters.Add("name", "meeting.ics"); var calendarView = AlternateView.CreateAlternateViewFromString(mailItem.VCalendarView, calendarType); calendarView.TransferEncoding = TransferEncoding.SevenBit; mail.AlternateViews.Add(calendarView); } #endregion #region Set Sender mail.From = mailItem.GetSender(); mail.ReplyToList.Add(mail.From); #endregion #region Set Receivers // Add To: foreach (var address in mailItem.To.Or("").Split(',').Trim().Where(a => IsSendingPermitted(a))) { mail.To.Add(address); } // Add Cc: foreach (var address in mailItem.Cc.Or("").Split(',').Trim().Where(a => IsSendingPermitted(a))) { mail.CC.Add(address); } foreach (var address in Config.Get("Email.Auto.CC.Address").Or("").Split(',').Trim().Where(a => IsSendingPermitted(a))) { mail.CC.Add(address); } // Add Bcc: foreach (var address in mailItem.Bcc.Or("").Split(',').Trim().Where(a => IsSendingPermitted(a))) { mail.Bcc.Add(address); } if (mail.To.None() && mail.CC.None() && mail.Bcc.None()) { return(null); } #endregion // Add attachments mail.Attachments.AddRange(mailItem.GetAttachments()); return(mail); }
string GetFrom(IEmailQueueItem email) => email.GetSender().Get(s => s.DisplayName.Or("").HtmlEncode() + s.Address.WithWrappers(" <", ">"));