void ErrorMail_Mailing(object sender, ErrorMailEventArgs e) { // if async is set to true, this.Context will be null if (this.Context == null) return; if (this.Context.Request == null) return; // change email subject in certain conditions if (String.Equals(this.Context.Request.Headers["X-Requested-With"], "XMLHttpRequest", StringComparison.OrdinalIgnoreCase)) e.Mail.Subject = "Ajax Error"; if (this.Context.Request.InputStream == null) return; if (!this.Context.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)) return; this.Context.Request.InputStream.Seek(0, SeekOrigin.Begin); var sr = new StreamReader(this.Context.Request.InputStream); var json = sr.ReadToEnd(); // will be empty string if InputStream was disposed var html = String.Format("<p>JSON:</p><pre style=\"background-color:#eee;\">{0}</pre>", json); e.Mail.Body = e.Mail.Body.Insert(e.Mail.Body.IndexOf("<pre id=\"errorDetail\">"), html); }
/// <summary> /// Fires the <see cref="DisposingMail"/> event. /// </summary> protected virtual void OnDisposingMail(ErrorMailEventArgs args) { if (args == null) { throw new ArgumentNullException("args"); } ErrorMailEventHandler handler = DisposingMail; if (handler != null) { handler(this, args); } }
/// <summary> /// Fires the <see cref="Mailed"/> event. /// </summary> protected virtual void OnMailed(ErrorMailEventArgs args) { if (args == null) { throw new ArgumentNullException("args"); } var handler = Mailed; if (handler != null) { handler(this, args); } }
/// <summary> /// Schedules the error to be e-mailed synchronously. /// </summary> protected virtual void ReportError(Error error) { if (error == null) { throw new ArgumentNullException("error"); } // // Start by checking if we have a sender and a recipient. // These values may be null if someone overrides the // implementation of OnInit but does not override the // MailSender and MailRecipient properties. // string sender = Mask.NullString(this.MailSender); string recipient = Mask.NullString(this.MailRecipient); string copyRecipient = Mask.NullString(this.MailCopyRecipient); #if NET_1_0 || NET_1_1 // // The sender can be defaulted in the <system.net> settings in 2.0 // if (sender.Length == 0) { return; } #endif if (recipient.Length == 0) { return; } // // Create the mail, setting up the sender and recipient and priority. // MailMessage mail = new MailMessage(); mail.Priority = this.MailPriority; #if NET_1_0 || NET_1_1 mail.From = sender; mail.To = recipient; if (copyRecipient.Length > 0) { mail.Cc = copyRecipient; } #else mail.From = new MailAddress(sender); mail.To.Add(recipient); if (copyRecipient.Length > 0) { mail.CC.Add(copyRecipient); } #endif // // Format the mail subject. // string subjectFormat = Mask.EmptyString(this.MailSubjectFormat, "Error ({1}): {0}"); mail.Subject = string.Format(subjectFormat, error.Message, error.Type). Replace('\r', ' ').Replace('\n', ' '); // // Format the mail body. // ErrorTextFormatter formatter = CreateErrorFormatter(); StringWriter bodyWriter = new StringWriter(); formatter.Format(bodyWriter, error); mail.Body = bodyWriter.ToString(); switch (formatter.MimeType) { #if NET_1_0 || NET_1_1 case "text/html": mail.BodyFormat = MailFormat.Html; break; case "text/plain": mail.BodyFormat = MailFormat.Text; break; #else case "text/html": mail.IsBodyHtml = true; break; case "text/plain": mail.IsBodyHtml = false; break; #endif default: { throw new ApplicationException(string.Format( "The error mail module does not know how to handle the {1} media type that is created by the {0} formatter.", formatter.GetType().FullName, formatter.MimeType)); } } #if NET_1_1 // // If the mail needs to be delivered to a particular SMTP server // then set-up the corresponding CDO configuration fields of the // mail message. // string smtpServer = Mask.NullString(this.SmtpServer); if (smtpServer.Length > 0) { IDictionary fields = mail.Fields; fields.Add(CdoConfigurationFields.SendUsing, /* cdoSendUsingPort */ 2); fields.Add(CdoConfigurationFields.SmtpServer, smtpServer); int smtpPort = this.SmtpPort; fields.Add(CdoConfigurationFields.SmtpServerPort, smtpPort <= 0 ? 25 : smtpPort); // // If the SMTP server requires authentication (indicated by // non-blank user name and password settings) then set-up // the corresponding CDO configuration fields of the mail // message. // string userName = Mask.NullString(this.AuthUserName); string password = Mask.NullString(this.AuthPassword); if (userName.Length > 0 && password.Length > 0) { fields.Add(CdoConfigurationFields.SmtpAuthenticate, 1); fields.Add(CdoConfigurationFields.SendUserName, userName); fields.Add(CdoConfigurationFields.SendPassword, password); } } #endif MailAttachment ysodAttachment = null; ErrorMailEventArgs args = new ErrorMailEventArgs(error, mail); try { // // If an HTML message was supplied by the web host then attach // it to the mail if not explicitly told not to do so. // if (!NoYsod && error.WebHostHtmlMessage.Length > 0) { ysodAttachment = CreateHtmlAttachment("YSOD", error.WebHostHtmlMessage); if (ysodAttachment != null) { mail.Attachments.Add(ysodAttachment); } } // // Send off the mail with some chance to pre- or post-process // using event. // OnMailing(args); SendMail(mail); OnMailed(args); } finally { #if NET_1_0 || NET_1_1 // // Delete any attached files, if necessary. // if (ysodAttachment != null) { File.Delete(ysodAttachment.Filename); mail.Attachments.Remove(ysodAttachment); } #endif OnDisposingMail(args); #if !NET_1_0 && !NET_1_1 mail.Dispose(); #endif } }
/// <summary> /// Schedules the error to be e-mailed synchronously. /// </summary> protected virtual void ReportError(Error error) { if (error == null) throw new ArgumentNullException("error"); // // Start by checking if we have a sender and a recipient. // These values may be null if someone overrides the // implementation of OnInit but does not override the // MailSender and MailRecipient properties. // string sender = Mask.NullString(this.MailSender); string recipient = Mask.NullString(this.MailRecipient); string copyRecipient = Mask.NullString(this.MailCopyRecipient); #if NET_1_0 || NET_1_1 // // The sender can be defaulted in the <system.net> settings in 2.0 // if (sender.Length == 0) return; #endif if (recipient.Length == 0) return; // // Create the mail, setting up the sender and recipient and priority. // MailMessage mail = new MailMessage(); mail.Priority = this.MailPriority; #if NET_1_0 || NET_1_1 mail.From = sender; mail.To = recipient; if (copyRecipient.Length > 0) mail.Cc = copyRecipient; #else mail.From = new MailAddress(sender); mail.To.Add(recipient); if (copyRecipient.Length > 0) mail.CC.Add(copyRecipient); #endif // // Format the mail subject. // string subjectFormat = Mask.EmptyString(this.MailSubjectFormat, "Error ({1}): {0} in {2}"); mail.Subject = string.Format(subjectFormat, error.Message, error.Type, error.ApplicationName). Replace('\r', ' ').Replace('\n', ' '); // // Format the mail body. // ErrorTextFormatter formatter = CreateErrorFormatter(); StringWriter bodyWriter = new StringWriter(); formatter.Format(bodyWriter, error); mail.Body = bodyWriter.ToString(); switch (formatter.MimeType) { #if NET_1_0 || NET_1_1 case "text/html" : mail.BodyFormat = MailFormat.Html; break; case "text/plain" : mail.BodyFormat = MailFormat.Text; break; #else case "text/html": mail.IsBodyHtml = true; break; case "text/plain": mail.IsBodyHtml = false; break; #endif default : { throw new ApplicationException(string.Format( "The error mail module does not know how to handle the {1} media type that is created by the {0} formatter.", formatter.GetType().FullName, formatter.MimeType)); } } #if NET_1_1 // // If the mail needs to be delivered to a particular SMTP server // then set-up the corresponding CDO configuration fields of the // mail message. // string smtpServer = Mask.NullString(this.SmtpServer); if (smtpServer.Length > 0) { IDictionary fields = mail.Fields; fields.Add(CdoConfigurationFields.SendUsing, /* cdoSendUsingPort */ 2); fields.Add(CdoConfigurationFields.SmtpServer, smtpServer); int smtpPort = this.SmtpPort; fields.Add(CdoConfigurationFields.SmtpServerPort, smtpPort <= 0 ? 25 : smtpPort); // // If the SMTP server requires authentication (indicated by // non-blank user name and password settings) then set-up // the corresponding CDO configuration fields of the mail // message. // string userName = Mask.NullString(this.AuthUserName); string password = Mask.NullString(this.AuthPassword); if (userName.Length > 0 && password.Length > 0) { fields.Add(CdoConfigurationFields.SmtpAuthenticate, 1); fields.Add(CdoConfigurationFields.SendUserName, userName); fields.Add(CdoConfigurationFields.SendPassword, password); } } #endif MailAttachment ysodAttachment = null; ErrorMailEventArgs args = new ErrorMailEventArgs(error, mail); try { // // If an HTML message was supplied by the web host then attach // it to the mail if not explicitly told not to do so. // if (!NoYsod && error.WebHostHtmlMessage.Length > 0) { ysodAttachment = CreateHtmlAttachment("YSOD", error.WebHostHtmlMessage); if (ysodAttachment != null) mail.Attachments.Add(ysodAttachment); } // // Send off the mail with some chance to pre- or post-process // using event. // OnMailing(args); SendMail(mail); OnMailed(args); } finally { #if NET_1_0 || NET_1_1 // // Delete any attached files, if necessary. // if (ysodAttachment != null) { File.Delete(ysodAttachment.Filename); mail.Attachments.Remove(ysodAttachment); } #endif OnDisposingMail(args); #if !NET_1_0 && !NET_1_1 mail.Dispose(); #endif } }
/// <summary> /// Fires the <see cref="Mailing"/> event. /// </summary> protected virtual void OnMailing(ErrorMailEventArgs args) { if (args == null) throw new ArgumentNullException("args"); ErrorMailEventHandler handler = Mailing; if (handler != null) handler(this, args); }
public void MailModuleMailed(object sender, ErrorMailEventArgs args) { var context = HttpContext.Current; if (context == null) return; var saz = context.Items[sazFilenameKey] as string; if (saz == null) return; var attachment = context.Items[attachmentKey] as Attachment; if (attachment != null) attachment.Dispose(); File.Delete(saz); }
public void MailModuleMailing(object sender, ErrorMailEventArgs args) { var context = HttpContext.Current; if (context == null) return; var attachment = MailModuleMailing(new HttpContextWrapper(context), config); if (attachment != null) args.Mail.Attachments.Add(attachment); }
/// <summary> /// Schedules the error to be e-mailed synchronously. /// </summary> protected virtual void ReportError(Error error) { if (error == null) throw new ArgumentNullException("error"); // // Start by checking if we have a sender and a recipient. // These values may be null if someone overrides the // implementation of OnInit but does not override the // MailSender and MailRecipient properties. // string sender = this.MailSender ?? string.Empty; string recipient = this.MailRecipient ?? string.Empty; string copyRecipient = this.MailCopyRecipient ?? string.Empty; if (recipient.Length == 0) return; // // Create the mail, setting up the sender and recipient and priority. // MailMessage mail = new MailMessage(); mail.Priority = this.MailPriority; mail.From = new MailAddress(sender); mail.To.Add(recipient); if (copyRecipient.Length > 0) mail.CC.Add(copyRecipient); // // Format the mail subject. // string subjectFormat = Mask.EmptyString(this.MailSubjectFormat, "Error ({1}): {0}"); mail.Subject = string.Format(subjectFormat, error.Message, error.Type). Replace('\r', ' ').Replace('\n', ' '); // // Format the mail body. // ErrorTextFormatter formatter = CreateErrorFormatter(); StringWriter bodyWriter = new StringWriter(); formatter.Format(bodyWriter, error); mail.Body = bodyWriter.ToString(); switch (formatter.MimeType) { case "text/html": mail.IsBodyHtml = true; break; case "text/plain": mail.IsBodyHtml = false; break; default : { throw new ApplicationException(string.Format( "The error mail module does not know how to handle the {1} media type that is created by the {0} formatter.", formatter.GetType().FullName, formatter.MimeType)); } } MailAttachment ysodAttachment = null; ErrorMailEventArgs args = new ErrorMailEventArgs(error, mail); try { // // If an HTML message was supplied by the web host then attach // it to the mail if not explicitly told not to do so. // if (!NoYsod && error.WebHostHtmlMessage.Length > 0) { ysodAttachment = CreateHtmlAttachment("YSOD", error.WebHostHtmlMessage); if (ysodAttachment != null) mail.Attachments.Add(ysodAttachment); } // // Send off the mail with some chance to pre- or post-process // using event. // OnMailing(args); SendMail(mail); OnMailed(args); } finally { OnDisposingMail(args); mail.Dispose(); } }
/// <summary> /// Fires the <see cref="DisposingMail"/> event. /// </summary> protected virtual void OnDisposingMail(ErrorMailEventArgs args) { if (args == null) throw new ArgumentNullException("args"); var handler = DisposingMail; if (handler != null) handler(this, args); }
void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs e) { e.Mail.Subject = "BloomService error: " + e.Error.Exception.Message; }
/// <summary> /// Schedules the error to be e-mailed synchronously. /// </summary> protected virtual void ReportError(Error error) { if (error == null) { throw new ArgumentNullException("error"); } // // Start by checking if we have a sender and a recipient. // These values may be null if someone overrides the // implementation of OnInit but does not override the // MailSender and MailRecipient properties. // var sender = this.MailSender ?? string.Empty; var recipient = this.MailRecipient ?? string.Empty; var copyRecipient = this.MailCopyRecipient ?? string.Empty; if (recipient.Length == 0) { return; } // // Create the mail, setting up the sender and recipient and priority. // var mail = new MailMessage(); mail.Priority = this.MailPriority; mail.From = new MailAddress(sender); mail.To.Add(recipient); if (copyRecipient.Length > 0) { mail.CC.Add(copyRecipient); } // // Format the mail subject. // var subjectFormat = Mask.EmptyString(this.MailSubjectFormat, "Error ({1}): {0}"); mail.Subject = string.Format(subjectFormat, error.Message, error.Type). Replace('\r', ' ').Replace('\n', ' '); // // Format the mail body. // var formatter = CreateErrorFormatter(); var bodyWriter = new StringWriter(); formatter.Format(bodyWriter, error); mail.Body = bodyWriter.ToString(); switch (formatter.MimeType) { case "text/html": mail.IsBodyHtml = true; break; case "text/plain": mail.IsBodyHtml = false; break; default: { throw new ApplicationException(string.Format( "The error mail module does not know how to handle the {1} media type that is created by the {0} formatter.", formatter.GetType().FullName, formatter.MimeType)); } } var args = new ErrorMailEventArgs(error, mail); try { // // If an HTML message was supplied by the web host then attach // it to the mail if not explicitly told not to do so. // if (!NoYsod && error.WebHostHtmlMessage.Length > 0) { var ysodAttachment = CreateHtmlAttachment("YSOD", error.WebHostHtmlMessage); if (ysodAttachment != null) { mail.Attachments.Add(ysodAttachment); } } // // Send off the mail with some chance to pre- or post-process // using event. // OnMailing(args); SendMail(mail); OnMailed(args); } finally { OnDisposingMail(args); mail.Dispose(); } }
public void MailModuleMailed(object sender, ErrorMailEventArgs args) { var context = HttpContext.Current; if (context == null) return; MailModuleMailed(new HttpContextWrapper(context)); }