Catching and E-Mailing Exceptions

Certify and Increase Opportunity.
Be
Govt. Certified ASP.NET Programmer

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 );
}

Get industry recognized certification – Contact us

Menu