/// <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 = 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(); } }