Add a new post


Name  
Email*  
Subject  
Message
 
 
Protected by Clearscreen.SharpHIPTo prevent abuse from bots, please enter the text you see.:
 

* In order to reduce SPAM, all email addresses will be obfuscated so they cannot be read by spam bots.
For example: 'test@test.com' will become 'test (at) test dot com'.

How do I email a website exception
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