Global.asax file

  • Thread starter paul downing via DotNetMonster.com
  • Start date
P

paul downing via DotNetMonster.com

global file email send

--------------------------------------------------------------------------------

guys, i'm developing an application and want to send an email with an error if it occurs to the administrator... i tried the following below but can't get it working.

I did have it working a while ago...but have had no success with this.... any ideas?

heres the code (simplistic) from the global.asax file:

<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.text" %>
<%@ Import Namespace="System.web" %>
<%@ Import Namespace="System.web.sessionstate" %>
<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.Text" %>
<script language="VB" runat="server">

Public Class Global
Inherits System.Web.HttpApplication

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


Dim mail As New MailMessage()
Dim ErrorMessage = "The error description is as follows : " & Server.GetLastError.ToString
mail.To = "(e-mail address removed)"
mail.Subject = "Error in the Site"
mail.Priority = MailPriority.High
mail.BodyFormat = MailFormat.Text
mail.Body = ErrorMessage
SmtpMail.Send(mail)

End Sub

End Class



i have even tried a simple reponse.write in the application_error and can't seem to make it trigger! Can someone point me in the right direction?

thanks
 
G

Guest

hai paul,
Just Try giving SmtpServerName/IP it may help u

System.Web.Mail.MailMessage mailer = new System.Web.Mail.MailMessage();
mailer.To = ToAddress;
mailer.From = FromAddress;
mailer.Subject = Subject;
mailer.Body = Body;
SmtpMail.SmtpServer = SmtpServerName/IP
SmtpMail.Send(mailer);

Good Luck
Mahi
 
P

paul downing via DotNetMonster.com

thanks for the help guys....


i've now tried just this in my global.asax file and i just can't seem to trigger anything


<script language="VB">



Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
response.write("hello")
End Sub


</script>



any ideas?
 
J

Juan T. Llibre [MVP]

Paul,

You cannot use Response.Write in global.asax

Where (in what page) would
the Response object write "hello" to ?



Juan T. Llibre
===========
 
P

pete

can you post the code that causes the error (or add one). if the originating
error is contained within a try / catch statement. you may need to rethrow
it for the application error event to fire

Cheers, pete
 
J

Juan T. Llibre [MVP]

Referencing the Session, Request, Response or Page
objects within application scope generates an error.

Application_Error only fires when an unhandled
exception is thrown, so you can't just Response.write
from it

You could *define* what happens when an
unhandled exception occurs, but you can't
do anything else within the Application_Error event.

Errors that are handled with error trapping
code (such as Try/Catch blocks) will *not*
cause the Application_Error event to fire.

It's impossible for response.write("hello")
to occur.within Application_Error.




Juan T. Llibre
===========
 
P

paul downing via DotNetMonster.com

right...

heres my page to generate an error when i click on the button..

<%@ Page Language="VB" %>
<script runat="server">

' Insert page code here
'

Sub Button1_Click(sender As Object, e As EventArgs)

throw(new ArgumentNullException())

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
<!-- Insert content here -->
</form>
</body>
</html>
------------------------------------------------------------------>


and heres my global.asax file code....


<script language="VB">



Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
response.redirect("error.aspx")
End Sub


</script>
 
J

Juan T. Llibre [MVP]

Hi, Paul.

If all you want to do is redirect the user to an error page,
you're far better off using a custom error page.

See
http://msdn.microsoft.com/library/d...us/cpgenref/html/gngrfCustomerrorsSection.asp
and
http://msdn.microsoft.com/asp.net/a...l=/library/en-us/dnaspp/html/customerrors.asp

There's a great source code sampler at
http://download.microsoft.com/downl...4057-af24-9e1a5b34b3dc/CustomErrorsSample.msi
( 341KB )

There's a very simple walkthrough for custom error reporting at
http://www.c-sharpcorner.com/Code/2002/May/CustomErrorHandlingASPNEt.asp
It explains the basics without much ado.





Juan T. Llibre
===========
 
P

paul downing via DotNetMonster.com

i have managed to get my error email working.

But I can't seem to trigger my global.asax error function,no matter what I do!

The web config file is fine and works by redirecting on an error, but I need to be able to send the email from the global.asax file but it just doesn't seem to trigger at all.

Any ideas?
 
R

Ravichandran J.V.

P

paul downing via DotNetMonster.com

thanks for the help guys,

the problem was a simple one

my global file was not in the root folder... i thought it was but it wasn't ... the folder i was using need to bee set to an application is IIS on the server.

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top