a web design blog.

Friday, September 5, 2008

Sending Error/Exception Reports via Email in ASP.net and C# (C-sharp)

I noticed we were getting a few keyword hits on error/exception report generation and sending error reports via email, so I have decided that today's entry will be just that. Below, you will find the code necessary for generating an email containing an error (exception) report using global.asax.

Firstly, enter this code in to the appropriate area, within global.asax file:
NOTE: Sorry about the poor formating (Blogger doesn't seem to handle code very well).


void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();

System.Text.StringBuilder sbMessage = new System.Text.StringBuilder();
sbMessage.Append("<dl style=\"font-face: Arial; font-weight: normal; font-size: 13px; line-height: 16px;\">");
sbMessage.Append("
<dt style=\"font-weight: bold;\">Source:</dt>");
sbMessage.AppendFormat("
<dd>{0}</dd>", exception.Source);
sbMessage.Append("
<dt style=\"font-weight: bold;\">Date and Time:</dt>");
sbMessage.AppendFormat("
<dd>{0}</dd>", DateTime.Now.ToString("MM/dd/yyyy h:mm tt"));
sbMessage.Append("
<dt style=\"font-weight: bold;\">Message:</dt>");
sbMessage.AppendFormat("
<dd>{0}</dd>", exception.Message);
sbMessage.Append("
<dt style=\"font-weight: bold;\">Stack Trace:</dt>");
sbMessage.AppendFormat("
<dd>{0}</dd>", exception.StackTrace);
sbMessage.Append("
</dl>");

using (System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage())
{
mailMessage.From = new System.Net.Mail.MailAddress(ConfigurationManager.AppSettings["emailWebsite"].ToString());
mailMessage.To.Add(new System.Net.Mail.MailAddress(ConfigurationManager.AppSettings["emailInformation"].ToString()));
mailMessage.Subject = "[ERROR] Unhandled Exception from the Website";
mailMessage.Body = sbMessage.ToString();
mailMessage.IsBodyHtml = true;

System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Host = ConfigurationManager.AppSettings["mailServerOutgoing"].ToString());
smtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["emailWebsite"].ToString(), ConfigurationManager.AppSettings["emailWebsitePassword"].ToString());
smtpClient.Send(mailMessage);
}
}



Once this is completed, and modified to specification, we must add the following definitions to the web.config (in the appSettings element):


<appsettings>
<add key="mailServerOutgoing" value="mail.your-server-address.com">
<add key="emailWebsite" value="mailer@your-server-address.com">
<add key="emailWebsitePassword" value="your-mailer-password">
<add key="emailInformation" value="receiving@your-server-address.com">
</appsettings>



This allows us to keep important information, such as email accounts and passwords, separate from the codebehind file, and is thus moderately more secure.

And that is all there is to it! All we ask is that if you use our code, just take a few moments and digg this entry using the addThis panel below. Thank you.

...

1 Comments:

  • Nice article!

    I implemented this a long time ago for a commercial application.

    One thing I did was actually store the trace in the database so it could be brought up in an admin area as a issue ticket, as well as email.

    If I remember correctly, and this is an if, you have to be careful when using database access and sending emails from that location to handle errors in the error handler.

    For example, if your site was to generate an error and gets caught by the global handler, then attempts to send an email but for somereason that throws an error, it can get into an endless loop and take down IIS.

    I am not 100% sure on this, but I thought I would mention it.

    Great article though and I am sure it will help many!

    By Anonymous Rare Pearl Design, At October 7, 2008 9:54 AM  

Post a Comment



Links to this post:

Create a Link

<< Home