/// <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 rethrow = false, string[] attachments = null, SmtpDeliveryMethod?overrideDeliveryMethod = null, bool forcePreview = false) { Exception error = null; // Re-throw - throw the error again to catch by a caller var message = new MailMessage(); var smtp = new SmtpClientEx(); try { MailHelper.ApplyRecipients(message, @from, @to, cc, bcc); MailHelper.ApplyAttachments(message, attachments); message.IsBodyHtml = isBodyHtml; message.Subject = subject; message.Body = body; // Override delivery method. if (overrideDeliveryMethod.HasValue) { smtp.DeliveryMethod = overrideDeliveryMethod.Value; } SendMail(message, smtp, forcePreview); } catch (Exception ex) { if (!ex.Data.Contains("Mail.DeliveryMethod")) { ex.Data.Add("Mail.DeliveryMethod", overrideDeliveryMethod); } if (!ex.Data.Contains("Mail.From")) { ex.Data.Add("Mail.From", @from); } 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 { Current.ProcessException(ex); } error = ex; } finally { // Attachments and message will be disposed. message.Dispose(); smtp.Dispose(); } return(error); }