Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Get the error details Dim lastErrorWrapper As HttpException = CType(Server.GetLastError(), HttpException) Dim lastError As Exception = lastErrorWrapper If lastErrorWrapper.InnerException IsNot Nothing Then lastError = lastErrorWrapper.InnerException End If Dim lastErrorTypeName As String = lastError.GetType().ToString() Dim lastErrorMessage As String = lastError.Message Dim lastErrorStackTrace As String = lastError.StackTrace Const ToAddress As String = "support@example.com" Const FromAddress As String = "support@example.com" Const Subject As String = "An Error Has Occurred!" ' Create the MailMessage object Dim mm As New MailMessage(FromAddress, ToAddress) mm.Subject = Subject mm.IsBodyHtml = True mm.Priority = MailPriority.High mm.Body = string.Format( _ "" & vbCrLf & _ " " & vbCrLf & _ "

An Error Has Occurred!

" & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " URL:" & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " User:" & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " Exception Type:" & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " Message:" & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " Stack Trace:" & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ "
{0}
{1}
{2}
{3}
{4}
" & vbCrLf & _ " " & vbCrLf & _ "", _ Request.RawUrl, _ User.Identity.Name, _ lastErrorTypeName, _ lastErrorMessage, _ lastErrorStackTrace.Replace(Environment.NewLine, "
")) 'Attach the Yellow Screen of Death for this error Dim YSODmarkup As String = lastErrorWrapper.GetHtmlErrorMessage() If Not String.IsNullOrEmpty(YSODmarkup) Then Dim YSOD As Attachment = Attachment.CreateAttachmentFromString(YSODmarkup, "YSOD.htm") mm.Attachments.Add(YSOD) End If ' Send the email Dim smtp As New SmtpClient() smtp.Send(mm) End Sub