Home ASP.NET Hosting Dedicated Servers Contact Us
Announcements
Virtual Tiers
.NET Applications
XML Web Services
SQL Server Database
Web Traffic Statistics
Much More...
ASP.NET Web Site Hosting
Dedicated Servers
Windows 2003 Server
2.4 GHz Pentium 4
1024 MB RAM
80 GB Hard Drive
1000 GB/month
Fully Managed
Free Setup!
$268.00/month
Windows Dedicated Servers
Specialized Plans
10 or more Domains
Windows Services
Custom Plans
Windows Services
Learning & Support
About Us
ASP.NET Tutorial Web Sites
123aspx.com
411asp.net
dotnetfreaks.com
wwwcoder.com



ASP.NET Tutorial: Global Error Handling using Global.asax and System.Web.Mail



Learn More about Server Intellect



Download the Visual Studio.NET 2003 VB.NET Project Here

More ASP.NET Tutorials

This tutorial will show you how to receive a nicely formatted HTML alert email with every exception thrown in your ASP.NET application.



In this tutorial we will be working with Global.asax.vb.

First, you will need to import the System.Text and System.Web.Mail namespace at the top of Global.asax.vb.

The System.Text namespace contains the StringBuilder Class which we will use to build our HTML email message. The System.Web.Mail namespace contains the SmtpMail Class.

Imports System.Web.Mail
Imports System.Text

We use the Application_Error method to catch any errors in our application. This method will send a nicely formatted HTML alert email to any address we specify.

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim msg1 As New MailMessage
msg1.From = "error@yourdomain.com"
msg1.To = "error@yourdomain.com"
msg1.Subject = "Page Error"
msg1.Body = BuildMessage()
msg1.BodyFormat = MailFormat.Html
msg1.Priority = MailPriority.Normal
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(msg1)
Catch ex As Exception
End Try
End Sub

Notice that we call the BuildMessage() function to build our message using the code below.

The flow for the code in Global.asax.vb is as follows.

Imports System.Web
Imports System.Web.Mail
Imports System.Text
Imports System.Web.SessionState

Public Class Global
Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub

#End Region

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
End Sub

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
End Sub

Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim msg1 As New MailMessage
msg1.From = "error@yourdomain.com"
msg1.To = "error@yourdomain.com"
msg1.Subject = "Page Error"
msg1.Body = BuildMessage()
msg1.BodyFormat = MailFormat.Html
msg1.Priority = MailPriority.Normal
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(msg1)
Catch ex As Exception
End Try
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application ends
End Sub

Function BuildMessage() As String

Dim strMessage As New StringBuilder

strMessage.Append("<style type=""text/css"">")
strMessage.Append("<!--")
strMessage.Append(".basix {")
strMessage.Append("font-family: Verdana, Arial, Helvetica, sans-serif;")
strMessage.Append("font-size: 12px;")
strMessage.Append("}")
strMessage.Append(".header1 {")
strMessage.Append("font-family: Verdana, Arial, Helvetica, sans-serif;")
strMessage.Append("font-size: 12px;")
strMessage.Append("font-weight: bold;")
strMessage.Append("color: #000099;")
strMessage.Append("}")
strMessage.Append(".tlbbkground1 {")
strMessage.Append("background-color: #000099;")
strMessage.Append("}")
strMessage.Append("-->")
strMessage.Append("</style>")

strMessage.Append("<table width=""85%"" border=""0"" align=""center"" cellpadding=""5"" cellspacing=""1"" class=""tlbbkground1"">")
strMessage.Append("<tr bgcolor=""#eeeeee"">")
strMessage.Append("<td colspan=""2"" class=""header1"">Page Error</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>IP Address</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.ServerVariables("REMOTE_ADDR") & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>User Agent</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.ServerVariables("HTTP_USER_AGENT") & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>Page</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.Url.AbsoluteUri & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>Time</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & System.DateTime.Now & " EST</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>Details</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Server.GetLastError().InnerException.ToString() & "</td>")
strMessage.Append("</tr>")
strMessage.Append("</table>")
Return strMessage.ToString
End Function

End Class


Download the Visual Studio.NET 2003 VB.NET Project Here

Do you have an ASP.NET Tutorial you would like to see here? Contact Us!