Introduction
One of the common uses for aspNetEmail is to send exceptions that occur on a
website to server administrators. aspNetEmail now provides functionality to
automatically format exceptions and send ASP.NET related information to the
receiver. aspNetEmail can send these exceptions as either Html or Text formatted
emails. For example, here is a screen shot of a Html formatted email of an
exception sent by aspNetEmail. (Click on the image below to view the email
better).
Code Example
The amount of code required to send this email is surprisingly short. In
fact it is only two lines. Here is an example (based upon web.config settings):
[web.config]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="EmailMessage.Server" value="mail.mycompany.com"></add>
<add key="EmailMessage.FromAddress" value="webmaster@mycompany.com"></add>
<add key="EmailMessage.To" value="webmaster@mycompany.com"></add>
<add key="EmailMessage.Subject" value="Error -- {0} "></add>
</appSettings>
<system.web>
</system.web>
</configuration>
[C#]
private void Page_Load(object sender, System.EventArgs e)
{
try
{
//do some work
int foo = int.Parse( "bar" );//oops, an exception will occur
}
catch(Exception ex )
{
//load the values from the web.config
EmailMessage msg = new EmailMessage( true, false );
//automatically format and send the exception
msg.FormatException( MailFormat.Html, ex, true, ServerErrorSection.All);
}
}
[VB.NET]
Private Sub Page_Load(sender As Object, e As System.EventArgs)
Try
'do some work
Dim foo As Integer = Integer.Parse("bar") 'oops, an exception will occur
Catch ex As Exception
'load the values from the web.config
Dim msg As New EmailMessage(True, False)
'automatically format and send the exception
msg.FormatException(MailFormat.Html, ex, True, ServerErrorSection.All)
End Try
End Sub 'Page_Load
Application Error Handler
Once you’ve worked with this code, and able to get it working, you can implement
a global error handler for your website. All you need to do is more the
FormatException() code into your global.asax Application_Error event handler.
Its important to make sure your code is working BEFORE you move it to
Application_Error, because Application_Error does not throw any exceptions.
Thus, if there is an error in your code, you will not realize it is happing, and
therefore emails may not be sent.
Here is an example of implementing the same code inside of the global.asax
[C#]
protected void Application_Error(Object sender, EventArgs e)
{
//load the values from the web.config
EmailMessage msg = new EmailMessage( true, false );
//automatically format and send the exception
msg.FormatException( MailFormat.Html,
Server.GetLastError().GetBaseException(), true, ServerErrorSection.All);
}
[VB.NET]
Protected Sub Application_Error(sender As [Object], e As EventArgs) 'load the values from the web.config Dim msg As New EmailMessage(True, False) 'automatically format and send the exception msg.FormatException(MailFormat.Html, Server.GetLastError().GetBaseException(), True, ServerErrorSection.All) End Sub 'Application_Error
Conclusion
That’s it! That’s all there is to utilize aspNetEmail to automatically send
exceptions from your website.
For more information, be sure to check out the aspNetEmail Class Reference
section. Specifically the EmailMessage.FormatException() and the
EmailMessage.SendASPNETException() methods.