System.Web.Mail FAQ

Complete FAQ Listing


3.4 How do I email a website exception Printer Friendly   Email This FAQ   Discuss

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.
 
[ C# ]
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 = "me@mycompany.com";
	mail.From = "you@yourcompany.com";
	mail.Subject = "An exception occurred.";
	mail.Body = ex.ToString();
	SmtpMail.SmtpServer = "localhost";  //your real server goes here
	SmtpMail.Send( mail );
}

[ VB.NET ]
Protected Sub Application_Error(sender As [Object], e As EventArgs)
   Dim ex As Exception = Server.GetLastError()
   EmailException(ex)
End Sub 'Application_Error


Private Sub EmailException(ex As Exception)
   Dim mail As New MailMessage()
   mail.To = "me@mycompany.com"
   mail.From = "you@yourcompany.com"
   mail.Subject = "An exception occurred."
   mail.Body = ex.ToString()
   SmtpMail.SmtpServer = "localhost" 'your real server goes here
   SmtpMail.Send(mail)
End Sub 'EmailException

 

Discuss This FAQ Item
Got a question? Another Suggestion? Want to post your solution? Discuss it below.
New Post
  • Handling email address error    [ Reply ]
    • RE: Handling email address error    [ Reply ]
    • RE: Handling email address error    [ Reply ]
  • Server.GetLastError(); ?    [ Reply ]
    • RE: Server.GetLastError(); ?    [ Reply ]
  • Email authentication    [ Reply ]
  • emailing a website    [ Reply ]
    • RE: emailing a website    [ Reply ]
  • Email on Content Update    [ Reply ]