Question

I have set up custom error on my server and i'm redirecting to a page as shown below

<customErrors mode="On"> 
   <error statusCode="500" redirect="/servererror/default.aspx" /> 
</customErrors>

When it gets to the page servererror/default.aspx I need it to send an e-mail to me with the exception.message

Here's what i'm trying but it won't work

Sub Page_load(ByVal sender As Object, ByVal e As EventArgs)

     Dim LastError As Exception
     Dim ErrMessage As String

     LastError = Server.GetLastError()


        ErrMessage = LastError.Message


Dim Errormail = New MailMessage

     'Send email to me
     Errormail.To           = "email@email.co.uk"

     Errormail.From         = "servererror@email.co.uk"
     Errormail.Subject      = "Server Error Alert"
     Errormail.BodyFormat   = MailFormat.Text
     Errormail.Priority     = MailPriority.Normal
     Errormail.Body         = ErrMessage

     SmtpMail.SmtpServer    = "localhost"

     SmtpMail.Send(Errormail)

     Server.ClearError()
End Sub

Any help would be much appreciated

Thanks

Jamie

Was it helpful?

Solution

Look here: ASP.NET custom error page - Server.GetLastError() is null

you need to add the redirectmode:

<customErrors mode="On" redirectMode="ResponseRewrite"> 
   <error statusCode="500" redirect="/servererror/default.aspx" /> 
</customErrors>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top