private MailMessage GetMailMessage(Exception exception, string to) { var error = new Error(exception); var subject = error.Message.Replace("\r", string.Empty).Replace("\n", string.Empty); var html = true; var body = "An error occurred."; try { var writer = new StringWriter(); var formatter = new ErrorMailHtmlFormatter(); formatter.Format(writer, error); body = writer.ToString(); } catch (Exception) { // some exceptions may creep up before we have access to the html formatter html = false; var builder = new StringBuilder(body); builder.AppendLine(); builder.AppendLine(); builder.AppendLine(exception.Message); builder.AppendLine(); builder.AppendLine(exception.StackTrace); body = builder.ToString(); } return(new MailMessage(_from, to) { Subject = subject, IsBodyHtml = html, Body = body, }); }
public override void Run(Error error) { var message = new MailMessage(); Recipients.AssertNotNullOrEmpty("Recipients"); FormattedSubject.AssertNotNullOrEmpty("FormattedSubject"); FormattedBody.AssertNotNullOrEmpty("FormattedBody"); From.AssertNotNullOrEmpty("From"); var stringBuilder = new StringBuilder(FormatString(FormattedBody, error)); using (var htmlTextWriter = new HtmlTextWriter(new StringWriter(stringBuilder))) { _errorMailHtmlFormatter.Format(htmlTextWriter, error); } message.From = new MailAddress(From); Recipients.Split(new char[] { ',' }) .Where(x => !string.IsNullOrEmpty(x)) .ToList() .ForEach(x => message.To.Add(x)); (CcRecipients ?? "").Split(new char[] { ',' }) .Where(x => !string.IsNullOrEmpty(x)) .ToList() .ForEach(x => message.To.Add(x)); message.Subject = FormatString(FormattedSubject, error); message.Body = stringBuilder.ToString(); message.IsBodyHtml = true; var client = new SmtpClient(); client.EnableSsl = IsTlsEnabled; client.Send(message); }
public void LogException(Exception exception) { var error = new Error(exception); // first try to post it to the Elmah log try { var log = ErrorLog.GetDefault(null); if (log != null) { log.Log(error); } } catch (Exception ex) { Trace.WriteLine(string.Format("Failed to add exception to Elmah error logger: {0}", ex.Message), "Error"); } // second try to send it via Elmah mail try { var writer = new StringWriter(); var formatter = new ErrorMailHtmlFormatter(); formatter.Format(writer, error); var message = new MailMessage(_config.EmailDefaultFromAddress, _config.EmailEmergencyAddresses) { Subject = error.Message.Replace("\r", string.Empty).Replace("\n", string.Empty), IsBodyHtml = true, Body = writer.ToString(), }; var client = new SmtpClient(); client.Send(message); } catch (Exception ex) { Trace.WriteLine(string.Format("Failed to send Elmah exception email: {0}", ex.Message), "Error"); } }
private MailMessage CreateMailMessage(Exception exception) { var error = new Error(exception); var subject = error.Message.Replace("\r", string.Empty).Replace("\n", string.Empty); var isHtml = true; var body = "An error occurred."; try { var writer = new StringWriter(); var formatter = new ErrorMailHtmlFormatter(); formatter.Format(writer, error); body = writer.ToString(); } catch (Exception) { // some exceptions may creep up before we have access to the html formatter isHtml = false; var builder = new StringBuilder(body); builder.AppendLine(); builder.AppendLine(); builder.AppendLine(exception.Message); builder.AppendLine(); builder.AppendLine(exception.StackTrace); body = builder.ToString(); } var tos = _appConfiguration.MailExceptionTo.ToArray(); var mailMessage = new MailMessage(_appConfiguration.MailFromDefault, tos.First()) { Subject = subject, Body = body, IsBodyHtml = isHtml, }; foreach (var to in tos.Skip(1)) { mailMessage.To.Add(to); } return(mailMessage); }