Click here to Skip to main content
11,584,208 members (54,058 online)
Click here to Skip to main content

Tagged as

Send ASP.NET formatted exception details via email

, 9 Oct 2011 CPOL 35.9K 21
Rate this:
Please Sign up or sign in to vote.
How to send ASP.NET formatted exception details via email

A small library named AnLogger has been created based on the concept described in this Tips. Please have a look AnLogger - ASP.Net Logger[^].

Managing Exception/Error is a common task in any application. In ASP.NET, there are many ways to handle those errors or exceptions, for example, when an error or exception occurrs in an ASP.NET application, the system is going to send a formatted error message to specified developers or teams mentioned in the email list. The difference will be the body of the email will contain exactly the same formatted error as the ASP.NET generated one to display the error or exception details when occurred. For example,

ASP.NET Exception Details

But when an ASP.NET application goes into life, the user should not have to see the above error page where they should see a custom error page which is easy to understand. More information is available here[^].

So in here, the test application will display the default error page when an error occurs in the system as well as send an email with the formatted exception details like in the above image.

To do that, we need to add the following code in the void Application_Error(object sender, EventArgs e) method to the Global.asax.cs file:

void Application_Error(object sender, EventArgs e)
{
    HttpUnhandledException httpUnhandledException = 
       new HttpUnhandledException(Server.GetLastError().Message, Server.GetLastError());
    SendEmailWithErrors(httpUnhandledException.GetHtmlErrorMessage());
}

So the GetHtmlErrorMessage() method of the HttpUnhandledException class will do all the formatting stuff. The HttpUnhandledException is .NET framework class and to find out more about it, please visit here[^].

The code block to send the email is shown below:

private static void SendEmailWithErrors(string result)
{
    try
    {
        MailMessage Message = new MailMessage();
        Message.To = "To address";
        Message.From = "FROM addressd";
        Message.Subject = "Exception raised";
        Message.BodyFormat = MailFormat.Html;
        Message.Body = result;
        SmtpMail.SmtpServer = "SMTP Sever Address";
        SmtpMail.Send(Message);
    }
    catch (System.Web.HttpException ehttp)
    {
        // Write o the event log.
    }
}

To test this, there is a button on the page which triggers an exception; the code for the button is as below:

protected void btnError_Click(object sender, EventArgs e)
{
    throw new Exception("This Exception is raised to test");
}

The system will display the default error page when an exception occurs and send an email with the formatted exception details as below:

Default error page:

User defined default error page

And an email with the formatted exception details:

Test Email with formatted exception details


Please have a look here to get similar Source code[^].

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Comments and Discussions

 
QuestionReason my Vote 5 Pin
_John1974_4-Mar-12 18:32
member_John1974_4-Mar-12 18:32 
GeneralReason for my vote of 4 Straight to the point and logical Pin
BrianBissell20-Oct-11 8:26
memberBrianBissell20-Oct-11 8:26 
GeneralRe: Thanks :) Pin
Mohammad A Rahman20-Oct-11 10:28
memberMohammad A Rahman20-Oct-11 10:28 
GeneralReason for my vote of 5 thanks for the tip Pin
beginner201111-Oct-11 15:42
memberbeginner201111-Oct-11 15:42 
GeneralRe: Thank you :) Pin
Mohammad A Rahman11-Oct-11 19:15
memberMohammad A Rahman11-Oct-11 19:15 
Generalyou might want to publish this is as a custom smtp appender ... Pin
Sazzad Hossain9-Oct-11 2:42
memberSazzad Hossain9-Oct-11 2:42 
GeneralRe: Thanks for the suggestions. I created a library named AnLogg... Pin
Mohammad A Rahman9-Oct-11 2:49
memberMohammad A Rahman9-Oct-11 2:49 
GeneralReason for my vote of 5 good Pin
prabhu.kanna.g3-Oct-11 19:58
memberprabhu.kanna.g3-Oct-11 19:58 
GeneralRe: Thanks Prabhu :) Pin
Mohammad A Rahman3-Oct-11 22:00
memberMohammad A Rahman3-Oct-11 22:00 
GeneralReason for my vote of 5 Thanks, for information Pin
Nik6627-Sep-11 22:34
memberNik6627-Sep-11 22:34 
GeneralRe: Thanks Nik66 :) Pin
Mohammad A Rahman27-Sep-11 23:11
memberMohammad A Rahman27-Sep-11 23:11 
GeneralWhy not just install and configure Elmah to send the emails? Pin
Daniel Gidman26-Sep-11 7:30
memberDaniel Gidman26-Sep-11 7:30 
GeneralRe: Good suggestion. This tips is just an another option to do t... Pin
Mohammad A Rahman26-Sep-11 11:15
memberMohammad A Rahman26-Sep-11 11:15 
Questionregarding HttpUnhandledException Pin
Tridip Bhattacharjee24-Sep-11 8:22
memberTridip Bhattacharjee24-Sep-11 8:22 
AnswerRe: regarding HttpUnhandledException Pin
Mohammad A Rahman24-Sep-11 12:11
memberMohammad A Rahman24-Sep-11 12:11 
GeneralRe: regarding HttpUnhandledException Pin
Tridip Bhattacharjee25-Sep-11 0:19
memberTridip Bhattacharjee25-Sep-11 0:19 
GeneralRe: regarding HttpUnhandledException Pin
Mohammad A Rahman25-Sep-11 1:50
memberMohammad A Rahman25-Sep-11 1:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 10 Oct 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid