public System.Net.Mail.MailMessage GetMailPreview(MailMessage message) { MailMessage mail = new MailMessage(); mail.IsBodyHtml = true; SmtpClientEx.ApplyRecipients(mail, message.From, Smtp.ErrorRecipients); var subject = message.Subject; ApplyRunModeSuffix(ref subject); mail.Subject = subject; string testBody = ""; testBody += "In LIVE mode this email would be sent:<br />\r\n"; foreach (var item in message.To) { testBody += "To: " + System.Web.HttpUtility.HtmlEncode(item.ToString()) + "<br />\r\n"; } foreach (var item in message.CC) { testBody += "Cc: " + System.Web.HttpUtility.HtmlEncode(item.ToString()) + "<br />\r\n"; } foreach (var item in message.Bcc) { testBody += "Bcc: " + System.Web.HttpUtility.HtmlEncode(item.ToString()) + "<br />\r\n"; } testBody += "<hr />\r\n"; var attachments = message.Attachments; if (attachments != null && attachments.Count() > 0) { testBody += "These files would be attached:<br />\r\n"; if (attachments != null && attachments.Count() > 0) { for (int ctr = 0; ctr <= attachments.Count() - 1; ctr++) { string fileName = attachments[ctr].Name; if (fileName.Length > 3 && fileName.ToLower().Substring(fileName.Length - 4) == ".ics") { mail.Attachments.Add(attachments[ctr]); } testBody += " " + System.Web.HttpUtility.HtmlEncode(fileName); testBody += "<br />\r\n"; } } } if (message.IsBodyHtml) { testBody += message.Body; } else { testBody += "<pre>"; testBody += System.Web.HttpUtility.HtmlEncode(message.Body); testBody += "</pre>"; } mail.Body = testBody; return(mail); }
/// <summary> /// Mail will be sent to error recipient if not LIVE. /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="cc"></param> /// <param name="bcc"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <param name="isBodyHtml"></param> /// <param name="preview">Force preview on LIVE system.</param> /// <param name="rethrow">Throw exception if sending fails. Must be set to false when sending exceptions.</param> /// <param name="attachments"></param> public Exception SendMailFrom(string @from, string @to, string cc, string bcc, string subject, string body, bool isBodyHtml, bool preview, bool rethrow, Attachment[] attachments, SmtpDeliveryMethod DeliveryMethod = SmtpDeliveryMethod.Network) { // Re-throw - throw the error again to catch by a caller try { var mail = new MailMessage(); SmtpClientEx.ApplyRecipients(mail, @from, @to, cc, bcc); SmtpClientEx.ApplyAttachments(mail, attachments); mail.IsBodyHtml = isBodyHtml; mail.Subject = subject; mail.Body = body; if (!IsLive || preview) { mail = GetMailPreview(mail); } if (DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory) { Smtp.SmtpDeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; Smtp.SmtpPickupFolder = SmtpClientEx.Current.SmtpPickupFolder; } Smtp.SendMessage(mail); return(null); } catch (Exception ex) { if (!string.IsNullOrEmpty(@to) && !ex.Data.Contains("Mail.To")) { ex.Data.Add("Mail.To", @to); } if (!string.IsNullOrEmpty(cc) && !ex.Data.Contains("Mail.Cc")) { ex.Data.Add("Mail.Cc", cc); } if (!string.IsNullOrEmpty(bcc) && !ex.Data.Contains("Mail.Bcc")) { ex.Data.Add("Mail.Bcc", bcc); } if (!string.IsNullOrEmpty(subject) && !ex.Data.Contains("Mail.Subject")) { ex.Data.Add("Mail.Subject", subject); } // Will be processed by the caller. if (rethrow) { throw; } else { ProcessException(ex); } return(ex); } }
/// <summary> /// Mail will be sent to error recipient if not LIVE. /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="cc"></param> /// <param name="bcc"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <param name="isBodyHtml"></param> /// <param name="preview">Force preview on LIVE system.</param> /// <param name="rethrow">Throw exception if sending fails. Must be set to false when sending exceptions.</param> /// <param name="attachments"></param> public void SendMailFrom(string @from, string @to, string cc, string bcc, string subject, string body, bool isBodyHtml, bool preview, bool rethrow, Attachment[] attachments) { // Re-throw - throw the error again to catch by a caller try { var mail = new MailMessage(); SmtpClientEx.ApplyRecipients(mail, @from, @to, cc, bcc); SmtpClientEx.ApplyAttachments(mail, attachments); mail.IsBodyHtml = isBodyHtml; mail.Subject = subject; mail.Body = body; if (!IsLive || preview) { mail = GetMailPreview(mail); } Smtp.SendMessage(mail); } catch (Exception ex) { if (!string.IsNullOrEmpty(@to) && !ex.Data.Contains("Mail.To")) { ex.Data.Add("Mail.To", @to); } if (!string.IsNullOrEmpty(cc) && !ex.Data.Contains("Mail.Cc")) { ex.Data.Add("Mail.Cc", cc); } if (!string.IsNullOrEmpty(bcc) && !ex.Data.Contains("Mail.Bcc")) { ex.Data.Add("Mail.Bcc", bcc); } if (!string.IsNullOrEmpty(subject) && !ex.Data.Contains("Mail.Subject")) { ex.Data.Add("Mail.Subject", subject); } // Will be processed by the caller. if (rethrow) { throw; } else { ProcessException(ex); } } }