Catching and E-Mailing Exceptions

Learning Resources
 

Catching and E-Mailing Exceptions


The global.asax class uses the Application_Error method to capture errors. Using System.Web.Mail, a webmaster can email themselves every time an error occurs.

The following code snippets demonstrates this technique.
 
protected void Application_Error(Object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    EmailException( ex );
}

private void EmailException( Exception ex )
{
    MailMessage mail = new MailMessage();
    mail.To = "[email protected]";
    mail.From = "[email protected]";
    mail.Subject = "An exception occurred.";
    mail.Body = ex.ToString();
    SmtpMail.SmtpServer = "localhost";  //your real server goes here
    SmtpMail.Send( mail );
}

 For Support