Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > SMTP Email Not Working from Application_Error handler in ASP.NET 2

Reply
Thread Tools

SMTP Email Not Working from Application_Error handler in ASP.NET 2

 
 
=?Utf-8?B?UEs5?=
Guest
Posts: n/a
 
      10-02-2007
I have an ASP.NET 2.0 Web Application. I am trying to send an email to
myself from Global.asax in the Application_Error event handler. I have been
able to successfully send emails from the rest of the application using the
exact same logic. For some reason, it does not work in global.asax. I get a
"Failure Sending Mail" error message with a more detailed explanation of
"Unable to read data from the transport connection: An existing connection
was forcibly closed by the remote host.". This is in a hosted environment,
and it IS working for other pages in the app (making me think I need to do
something different in the Application_Error function.

I am using the System.Net.Mail class. Below is my code and the detailed
error (notice I have some general information stored in web.config which is
pasted below as well):

GLOBAL.ASAX
--------------------------------------------------------
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError();
string strEmailAddressTo = "(E-Mail Removed)";
string strEmailAddressFrom = "(E-Mail Removed)";


string ErrorMessage = ex.Message +
"\nSOURCE: " + ex.Source +
"\nFORM: " + Request.Form.ToString() +
"\nQUERYSTRING: " +
Request.QueryString.ToString() +
"\nTARGETSITE: " + ex.TargetSite +
"\nSTACKTRACE: " + ex.StackTrace;

//1) Create the mail message instance
MailMessage mm = new MailMessage(strEmailAddressFrom,
strEmailAddressTo);

//2) Assign the Mail Message's Properties
// Subject
mm.Subject = "ERROR OCCURRED";

//3) Body
mm.Body = ErrorMessage.ToString();
mm.IsBodyHtml = false;

//4) Create the SMTP Client Object
SmtpClient smtp = new SmtpClient();


//5) Send the mail message (will use the web.config settings)
smtp.Send(mm);
}


web.config
------------------------------------------------------------
<system.net>
<mailSettings>
<smtp>
<network host="[mynetworkHost]" port="25" />
</smtp>
</mailSettings>
</system.net>

DETAILED ERROR MESSAGE:
-------------------------------------------------------------
<ExceptionInformation><AdditionalInformationProper ty
ExceptionManager.MachineName="XXXXX" ExceptionManager.TimeStamp="10/2/2007
1:32:25 AM"
ExceptionManager.FullName="Microsoft.ApplicationBl ocks.ExceptionManagement,
Version=1.0.2830.35366, Culture=neutral, PublicKeyToken=null"
ExceptionManager.AppDomainName="/LM/w3svc/814863/root-11-128357766911129800"
ExceptionManager.ThreadIdentity="" ExceptionManager.WindowsIdentity="NT
AUTHORITY\NETWORK SERVICE" /><Exception
ExceptionType="System.Net.Mail.SmtpException" StatusCode="GeneralFailure"
Message="Failure sending mail."
Data="System.Collections.ListDictionaryInternal" TargetSite="Void
Send(System.Net.Mail.MailMessage)" Source="System"><StackTrace> at
System.Net.Mail.SmtpClient.Send(MailMessage message)
in \\[webpath]\web\Global.asax:line 101</StackTrace><Exception
ExceptionType="System.IO.IOException" Message="Unable to read data from the
transport connection: An existing connection was forcibly closed by the
remote host." Data="System.Collections.ListDictionaryInternal"
TargetSite="Int32 Read(Byte[], Int32, Int32)" Source="System"><StackTrace>
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
size)
at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32
count)
at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32
count)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(S mtpReplyReader
caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(Sm tpReplyReader caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
at System.Net.Mail.DataStopCommand.Send(SmtpConnectio n conn)
at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args)
at System.Net.ClosableStream.Close()
at System.Net.Mail.MailWriter.Close()
at System.Net.Mail.SmtpClient.Send(MailMessage
message)</StackTrace><Exception
ExceptionType="System.Net.Sockets.SocketException" ErrorCode="10054"
SocketErrorCode="ConnectionReset" NativeErrorCode="10054" Message="An
existing connection was forcibly closed by the remote host"
Data="System.Collections.ListDictionaryInternal" TargetSite="Int32
Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)"
Source="System"><StackTrace> at System.Net.Sockets.Socket.Receive(Byte[]
buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32
size)</StackTrace></Exception></Exception></Exception></ExceptionInformation>


--
PK9
 
Reply With Quote
 
 
 
 
Aidy
Guest
Posts: n/a
 
      10-02-2007
The actual problem is probably the code that is throwing the exception not
tidying up its network connects, mail client etc, so that when the error
event in your global.asax fires your network connections are not in a
suitable condition to send e-mail. This will be doubly so if it is
e-mailing that is throwing your original exception.

"PK9" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have an ASP.NET 2.0 Web Application. I am trying to send an email to
> myself from Global.asax in the Application_Error event handler. I have
> been
> able to successfully send emails from the rest of the application using
> the
> exact same logic. For some reason, it does not work in global.asax. I
> get a
> "Failure Sending Mail" error message with a more detailed explanation of
> "Unable to read data from the transport connection: An existing connection
> was forcibly closed by the remote host.". This is in a hosted
> environment,
> and it IS working for other pages in the app (making me think I need to do
> something different in the Application_Error function.
>
> I am using the System.Net.Mail class. Below is my code and the detailed
> error (notice I have some general information stored in web.config which
> is
> pasted below as well):
>
> GLOBAL.ASAX
> --------------------------------------------------------
> void Application_Error(object sender, EventArgs e)
> {
> // Code that runs when an unhandled error occurs
> Exception ex = Server.GetLastError();
> string strEmailAddressTo = "(E-Mail Removed)";
> string strEmailAddressFrom = "(E-Mail Removed)";
>
>
> string ErrorMessage = ex.Message +
> "\nSOURCE: " + ex.Source +
> "\nFORM: " + Request.Form.ToString() +
> "\nQUERYSTRING: " +
> Request.QueryString.ToString() +
> "\nTARGETSITE: " + ex.TargetSite +
> "\nSTACKTRACE: " + ex.StackTrace;
>
> //1) Create the mail message instance
> MailMessage mm = new MailMessage(strEmailAddressFrom,
> strEmailAddressTo);
>
> //2) Assign the Mail Message's Properties
> // Subject
> mm.Subject = "ERROR OCCURRED";
>
> //3) Body
> mm.Body = ErrorMessage.ToString();
> mm.IsBodyHtml = false;
>
> //4) Create the SMTP Client Object
> SmtpClient smtp = new SmtpClient();
>
>
> //5) Send the mail message (will use the web.config settings)
> smtp.Send(mm);
> }
>
>
> web.config
> ------------------------------------------------------------
> <system.net>
> <mailSettings>
> <smtp>
> <network host="[mynetworkHost]" port="25" />
> </smtp>
> </mailSettings>
> </system.net>
>
> DETAILED ERROR MESSAGE:
> -------------------------------------------------------------
> <ExceptionInformation><AdditionalInformationProper ty
> ExceptionManager.MachineName="XXXXX" ExceptionManager.TimeStamp="10/2/2007
> 1:32:25 AM"
> ExceptionManager.FullName="Microsoft.ApplicationBl ocks.ExceptionManagement,
> Version=1.0.2830.35366, Culture=neutral, PublicKeyToken=null"
> ExceptionManager.AppDomainName="/LM/w3svc/814863/root-11-128357766911129800"
> ExceptionManager.ThreadIdentity="" ExceptionManager.WindowsIdentity="NT
> AUTHORITY\NETWORK SERVICE" /><Exception
> ExceptionType="System.Net.Mail.SmtpException" StatusCode="GeneralFailure"
> Message="Failure sending mail."
> Data="System.Collections.ListDictionaryInternal" TargetSite="Void
> Send(System.Net.Mail.MailMessage)" Source="System"><StackTrace> at
> System.Net.Mail.SmtpClient.Send(MailMessage message)
> in \\[webpath]\web\Global.asax:line 101</StackTrace><Exception
> ExceptionType="System.IO.IOException" Message="Unable to read data from
> the
> transport connection: An existing connection was forcibly closed by the
> remote host." Data="System.Collections.ListDictionaryInternal"
> TargetSite="Int32 Read(Byte[], Int32, Int32)" Source="System"><StackTrace>
> at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
> Int32
> size)
> at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32
> count)
> at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32
> count)
> at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(S mtpReplyReader
> caller, Boolean oneLine)
> at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(Sm tpReplyReader
> caller)
> at System.Net.Mail.SmtpReplyReader.ReadLine()
> at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String&
> response)
> at System.Net.Mail.DataStopCommand.Send(SmtpConnectio n conn)
> at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args)
> at System.Net.ClosableStream.Close()
> at System.Net.Mail.MailWriter.Close()
> at System.Net.Mail.SmtpClient.Send(MailMessage
> message)</StackTrace><Exception
> ExceptionType="System.Net.Sockets.SocketException" ErrorCode="10054"
> SocketErrorCode="ConnectionReset" NativeErrorCode="10054" Message="An
> existing connection was forcibly closed by the remote host"
> Data="System.Collections.ListDictionaryInternal" TargetSite="Int32
> Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)"
> Source="System"><StackTrace> at System.Net.Sockets.Socket.Receive(Byte[]
> buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
> at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
> Int32
> size)</StackTrace></Exception></Exception></Exception></ExceptionInformation>
>
>
> --
> PK9
>



 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 5:33 pm, PK9 <(E-Mail Removed)> wrote:
> I have an ASP.NET 2.0 Web Application. I am trying to send an email to
> myself from Global.asax in the Application_Error event handler. I have been
> able to successfully send emails from the rest of the application using the
> exact same logic. For some reason, it does not work in global.asax. I get a
> "Failure Sending Mail" error message with a more detailed explanation of
> "Unable to read data from the transport connection: An existing connection
> was forcibly closed by the remote host.". This is in a hosted environment,
> and it IS working for other pages in the app (making me think I need to do
> something different in the Application_Error function.
>
> I am using the System.Net.Mail class. Below is my code and the detailed
> error (notice I have some general information stored in web.config which is
> pasted below as well):
>
> GLOBAL.ASAX
> --------------------------------------------------------
> void Application_Error(object sender, EventArgs e)
> {
> // Code that runs when an unhandled error occurs
> Exception ex = Server.GetLastError();
> string strEmailAddressTo = "(E-Mail Removed)";
> string strEmailAddressFrom = "(E-Mail Removed)";
>
> string ErrorMessage = ex.Message +
> "\nSOURCE: " + ex.Source +
> "\nFORM: " + Request.Form.ToString() +
> "\nQUERYSTRING: " +
> Request.QueryString.ToString() +
> "\nTARGETSITE: " + ex.TargetSite +
> "\nSTACKTRACE: " + ex.StackTrace;
>
> //1) Create the mail message instance
> MailMessage mm = new MailMessage(strEmailAddressFrom,
> strEmailAddressTo);
>
> //2) Assign the Mail Message's Properties
> // Subject
> mm.Subject = "ERROR OCCURRED";
>
> //3) Body
> mm.Body = ErrorMessage.ToString();
> mm.IsBodyHtml = false;
>
> //4) Create the SMTP Client Object
> SmtpClient smtp = new SmtpClient();
>
> //5) Send the mail message (will use the web.config settings)
> smtp.Send(mm);
>
> }
>
> web.config
> ------------------------------------------------------------
> <system.net>
> <mailSettings>
> <smtp>
> <network host="[mynetworkHost]" port="25" />
> </smtp>
> </mailSettings>
> </system.net>
>
> DETAILED ERROR MESSAGE:
> -------------------------------------------------------------
> <ExceptionInformation><AdditionalInformationProper ty
> ExceptionManager.MachineName="XXXXX" ExceptionManager.TimeStamp="10/2/2007
> 1:32:25 AM"
> ExceptionManager.FullName="Microsoft.ApplicationBl ocks.ExceptionManagement,
> Version=1.0.2830.35366, Culture=neutral, PublicKeyToken=null"
> ExceptionManager.AppDomainName="/LM/w3svc/814863/root-11-128357766911129800*"
> ExceptionManager.ThreadIdentity="" ExceptionManager.WindowsIdentity="NT
> AUTHORITY\NETWORK SERVICE" /><Exception
> ExceptionType="System.Net.Mail.SmtpException" StatusCode="GeneralFailure"
> Message="Failure sending mail."
> Data="System.Collections.ListDictionaryInternal" TargetSite="Void
> Send(System.Net.Mail.MailMessage)" Source="System"><StackTrace> at
> System.Net.Mail.SmtpClient.Send(MailMessage message)
> in \\[webpath]\web\Global.asax:line 101</StackTrace><Exception
> ExceptionType="System.IO.IOException" Message="Unable to read data from the
> transport connection: An existing connection was forcibly closed by the
> remote host." Data="System.Collections.ListDictionaryInternal"
> TargetSite="Int32 Read(Byte[], Int32, Int32)" Source="System"><StackTrace>
> at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
> size)
> at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32
> count)
> at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32
> count)
> at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(S mtpReplyReader
> caller, Boolean oneLine)
> at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(Sm tpReplyReader caller)
> at System.Net.Mail.SmtpReplyReader.ReadLine()
> at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
> at System.Net.Mail.DataStopCommand.Send(SmtpConnectio n conn)
> at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args)
> at System.Net.ClosableStream.Close()
> at System.Net.Mail.MailWriter.Close()
> at System.Net.Mail.SmtpClient.Send(MailMessage
> message)</StackTrace><Exception
> ExceptionType="System.Net.Sockets.SocketException" ErrorCode="10054"
> SocketErrorCode="ConnectionReset" NativeErrorCode="10054" Message="An
> existing connection was forcibly closed by the remote host"
> Data="System.Collections.ListDictionaryInternal" TargetSite="Int32
> Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)"
> Source="System"><StackTrace> at System.Net.Sockets.Socket.Receive(Byte[]
> buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
> at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
> Int32
> size)</StackTrace></Exception></Exception></Exception></ExceptionInformatio*n>
>
> --
> PK9


1. How do you get that error message?
2. Did you tried to specify default credential in mailSettings?

 
Reply With Quote
 
=?Utf-8?B?UEs5?=
Guest
Posts: n/a
 
      10-02-2007
Thank you for your response. So how would one handle this? I assume that
catching errors in the global.asax and emailing a sys admin is typical
practice. You may be correct that the network connects are not in a suitable
condition to send email -- but I'm not sure how to remedy that.

Just for further information, this happens regardless of the type of error
thrown (i.e. dividebyzeroexception, custom applicationexception, etc). THe
email functionality will simply not work from the Application_Error event
handler.

Any ideas on how to remedy this?
--
PK9


"Aidy" wrote:

> The actual problem is probably the code that is throwing the exception not
> tidying up its network connects, mail client etc, so that when the error
> event in your global.asax fires your network connections are not in a
> suitable condition to send e-mail. This will be doubly so if it is
> e-mailing that is throwing your original exception.
>
> "PK9" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >I have an ASP.NET 2.0 Web Application. I am trying to send an email to
> > myself from Global.asax in the Application_Error event handler. I have
> > been
> > able to successfully send emails from the rest of the application using
> > the
> > exact same logic. For some reason, it does not work in global.asax. I
> > get a
> > "Failure Sending Mail" error message with a more detailed explanation of
> > "Unable to read data from the transport connection: An existing connection
> > was forcibly closed by the remote host.". This is in a hosted
> > environment,
> > and it IS working for other pages in the app (making me think I need to do
> > something different in the Application_Error function.
> >
> > I am using the System.Net.Mail class. Below is my code and the detailed
> > error (notice I have some general information stored in web.config which
> > is
> > pasted below as well):
> >
> > GLOBAL.ASAX
> > --------------------------------------------------------
> > void Application_Error(object sender, EventArgs e)
> > {
> > // Code that runs when an unhandled error occurs
> > Exception ex = Server.GetLastError();
> > string strEmailAddressTo = "(E-Mail Removed)";
> > string strEmailAddressFrom = "(E-Mail Removed)";
> >
> >
> > string ErrorMessage = ex.Message +
> > "\nSOURCE: " + ex.Source +
> > "\nFORM: " + Request.Form.ToString() +
> > "\nQUERYSTRING: " +
> > Request.QueryString.ToString() +
> > "\nTARGETSITE: " + ex.TargetSite +
> > "\nSTACKTRACE: " + ex.StackTrace;
> >
> > //1) Create the mail message instance
> > MailMessage mm = new MailMessage(strEmailAddressFrom,
> > strEmailAddressTo);
> >
> > //2) Assign the Mail Message's Properties
> > // Subject
> > mm.Subject = "ERROR OCCURRED";
> >
> > //3) Body
> > mm.Body = ErrorMessage.ToString();
> > mm.IsBodyHtml = false;
> >
> > //4) Create the SMTP Client Object
> > SmtpClient smtp = new SmtpClient();
> >
> >
> > //5) Send the mail message (will use the web.config settings)
> > smtp.Send(mm);
> > }
> >
> >
> > web.config
> > ------------------------------------------------------------
> > <system.net>
> > <mailSettings>
> > <smtp>
> > <network host="[mynetworkHost]" port="25" />
> > </smtp>
> > </mailSettings>
> > </system.net>
> >
> > DETAILED ERROR MESSAGE:
> > -------------------------------------------------------------
> > <ExceptionInformation><AdditionalInformationProper ty
> > ExceptionManager.MachineName="XXXXX" ExceptionManager.TimeStamp="10/2/2007
> > 1:32:25 AM"
> > ExceptionManager.FullName="Microsoft.ApplicationBl ocks.ExceptionManagement,
> > Version=1.0.2830.35366, Culture=neutral, PublicKeyToken=null"
> > ExceptionManager.AppDomainName="/LM/w3svc/814863/root-11-128357766911129800"
> > ExceptionManager.ThreadIdentity="" ExceptionManager.WindowsIdentity="NT
> > AUTHORITY\NETWORK SERVICE" /><Exception
> > ExceptionType="System.Net.Mail.SmtpException" StatusCode="GeneralFailure"
> > Message="Failure sending mail."
> > Data="System.Collections.ListDictionaryInternal" TargetSite="Void
> > Send(System.Net.Mail.MailMessage)" Source="System"><StackTrace> at
> > System.Net.Mail.SmtpClient.Send(MailMessage message)
> > in \\[webpath]\web\Global.asax:line 101</StackTrace><Exception
> > ExceptionType="System.IO.IOException" Message="Unable to read data from
> > the
> > transport connection: An existing connection was forcibly closed by the
> > remote host." Data="System.Collections.ListDictionaryInternal"
> > TargetSite="Int32 Read(Byte[], Int32, Int32)" Source="System"><StackTrace>
> > at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
> > Int32
> > size)
> > at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32
> > count)
> > at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32
> > count)
> > at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(S mtpReplyReader
> > caller, Boolean oneLine)
> > at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(Sm tpReplyReader
> > caller)
> > at System.Net.Mail.SmtpReplyReader.ReadLine()
> > at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String&
> > response)
> > at System.Net.Mail.DataStopCommand.Send(SmtpConnectio n conn)
> > at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args)
> > at System.Net.ClosableStream.Close()
> > at System.Net.Mail.MailWriter.Close()
> > at System.Net.Mail.SmtpClient.Send(MailMessage
> > message)</StackTrace><Exception
> > ExceptionType="System.Net.Sockets.SocketException" ErrorCode="10054"
> > SocketErrorCode="ConnectionReset" NativeErrorCode="10054" Message="An
> > existing connection was forcibly closed by the remote host"
> > Data="System.Collections.ListDictionaryInternal" TargetSite="Int32
> > Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)"
> > Source="System"><StackTrace> at System.Net.Sockets.Socket.Receive(Byte[]
> > buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
> > at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
> > Int32
> > size)</StackTrace></Exception></Exception></Exception></ExceptionInformation>
> >
> >
> > --
> > PK9
> >

>
>
>

 
Reply With Quote
 
=?Utf-8?B?UEs5?=
Guest
Posts: n/a
 
      10-02-2007
I get this problem any time an error is thrown within the application and the
Application_Error event handler is called. I have a site in production, and
to test the error handling in production, I have created a button with an
event handler that when clicked will throw an exception. I have tested it by
adding "throw new ApplicationException("This is a test exception being
thrown...");" and by throwing a named exception "throw new
DivideByZeroException();".

Both correctly go to the Application_Error event handler, but the email
portion fails everytime.

I'm at a loss.

Not sure which "default credentials" you're referring to. I have the
following settings specified in web.config, but I've tried to add additional
information such as username and password to the web.config as well (with no
luck):
web.config
------------------------------------------------------------
<system.net>
<mailSettings>
<smtp>
<network host="[mynetworkHost]" port="25" />
</smtp>
</mailSettings>
</system.net>

--
PK9


"Alexey Smirnov" wrote:

> On Oct 2, 5:33 pm, PK9 <(E-Mail Removed)> wrote:
> > I have an ASP.NET 2.0 Web Application. I am trying to send an email to
> > myself from Global.asax in the Application_Error event handler. I have been
> > able to successfully send emails from the rest of the application using the
> > exact same logic. For some reason, it does not work in global.asax. I get a
> > "Failure Sending Mail" error message with a more detailed explanation of
> > "Unable to read data from the transport connection: An existing connection
> > was forcibly closed by the remote host.". This is in a hosted environment,
> > and it IS working for other pages in the app (making me think I need to do
> > something different in the Application_Error function.
> >
> > I am using the System.Net.Mail class. Below is my code and the detailed
> > error (notice I have some general information stored in web.config which is
> > pasted below as well):
> >
> > GLOBAL.ASAX
> > --------------------------------------------------------
> > void Application_Error(object sender, EventArgs e)
> > {
> > // Code that runs when an unhandled error occurs
> > Exception ex = Server.GetLastError();
> > string strEmailAddressTo = "(E-Mail Removed)";
> > string strEmailAddressFrom = "(E-Mail Removed)";
> >
> > string ErrorMessage = ex.Message +
> > "\nSOURCE: " + ex.Source +
> > "\nFORM: " + Request.Form.ToString() +
> > "\nQUERYSTRING: " +
> > Request.QueryString.ToString() +
> > "\nTARGETSITE: " + ex.TargetSite +
> > "\nSTACKTRACE: " + ex.StackTrace;
> >
> > //1) Create the mail message instance
> > MailMessage mm = new MailMessage(strEmailAddressFrom,
> > strEmailAddressTo);
> >
> > //2) Assign the Mail Message's Properties
> > // Subject
> > mm.Subject = "ERROR OCCURRED";
> >
> > //3) Body
> > mm.Body = ErrorMessage.ToString();
> > mm.IsBodyHtml = false;
> >
> > //4) Create the SMTP Client Object
> > SmtpClient smtp = new SmtpClient();
> >
> > //5) Send the mail message (will use the web.config settings)
> > smtp.Send(mm);
> >
> > }
> >
> > web.config
> > ------------------------------------------------------------
> > <system.net>
> > <mailSettings>
> > <smtp>
> > <network host="[mynetworkHost]" port="25" />
> > </smtp>
> > </mailSettings>
> > </system.net>
> >
> > DETAILED ERROR MESSAGE:
> > -------------------------------------------------------------
> > <ExceptionInformation><AdditionalInformationProper ty
> > ExceptionManager.MachineName="XXXXX" ExceptionManager.TimeStamp="10/2/2007
> > 1:32:25 AM"
> > ExceptionManager.FullName="Microsoft.ApplicationBl ocks.ExceptionManagement,
> > Version=1.0.2830.35366, Culture=neutral, PublicKeyToken=null"
> > ExceptionManager.AppDomainName="/LM/w3svc/814863/root-11-128357766911129800-"
> > ExceptionManager.ThreadIdentity="" ExceptionManager.WindowsIdentity="NT
> > AUTHORITY\NETWORK SERVICE" /><Exception
> > ExceptionType="System.Net.Mail.SmtpException" StatusCode="GeneralFailure"
> > Message="Failure sending mail."
> > Data="System.Collections.ListDictionaryInternal" TargetSite="Void
> > Send(System.Net.Mail.MailMessage)" Source="System"><StackTrace> at
> > System.Net.Mail.SmtpClient.Send(MailMessage message)
> > in \\[webpath]\web\Global.asax:line 101</StackTrace><Exception
> > ExceptionType="System.IO.IOException" Message="Unable to read data from the
> > transport connection: An existing connection was forcibly closed by the
> > remote host." Data="System.Collections.ListDictionaryInternal"
> > TargetSite="Int32 Read(Byte[], Int32, Int32)" Source="System"><StackTrace>
> > at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
> > size)
> > at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32
> > count)
> > at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32
> > count)
> > at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(S mtpReplyReader
> > caller, Boolean oneLine)
> > at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(Sm tpReplyReader caller)
> > at System.Net.Mail.SmtpReplyReader.ReadLine()
> > at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
> > at System.Net.Mail.DataStopCommand.Send(SmtpConnectio n conn)
> > at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args)
> > at System.Net.ClosableStream.Close()
> > at System.Net.Mail.MailWriter.Close()
> > at System.Net.Mail.SmtpClient.Send(MailMessage
> > message)</StackTrace><Exception
> > ExceptionType="System.Net.Sockets.SocketException" ErrorCode="10054"
> > SocketErrorCode="ConnectionReset" NativeErrorCode="10054" Message="An
> > existing connection was forcibly closed by the remote host"
> > Data="System.Collections.ListDictionaryInternal" TargetSite="Int32
> > Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)"
> > Source="System"><StackTrace> at System.Net.Sockets.Socket.Receive(Byte[]
> > buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
> > at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
> > Int32
> > size)</StackTrace></Exception></Exception></Exception></ExceptionInformatio-n>
> >
> > --
> > PK9

>
> 1. How do you get that error message?
> 2. Did you tried to specify default credential in mailSettings?
>
>

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 10:53 pm, PK9 <(E-Mail Removed)> wrote:
> I get this problem any time an error is thrown within the application and the
> Application_Error event handler is called. I have a site in production, and
> to test the error handling in production, I have created a button with an
> event handler that when clicked will throw an exception. I have tested it by
> adding "throw new ApplicationException("This is a test exception being
> thrown...");" and by throwing a named exception "throw new
> DivideByZeroException();".
>
> Both correctly go to the Application_Error event handler, but the email
> portion fails everytime.
>
> I'm at a loss.
>
> Not sure which "default credentials" you're referring to. I have the
> following settings specified in web.config, but I've tried to add additional
> information such as username and password to the web.config as well (with no
> luck):
> web.config
> ------------------------------------------------------------
> <system.net>
> <mailSettings>
> <smtp>
> <network host="[mynetworkHost]" port="25" />
> </smtp>
> </mailSettings>
> </system.net>
>


Well, the code looks correct but I'm not sure what exactly that error
message means. I would try to simplify Application_Error to send just
a test email to see if it helps and I check if other part of code
where email is working has the same identity as NT AUTHORITY\NETWORK
SERVICE (it stays in exception)

Response.Write(Environment.UserName);

Maybe you use an impersonation there?

BTW, check ASP.NET Health Monitoring, it can do the same using
standard web.config configuration.
http://msdn2.microsoft.com/en-us/library/ms178701.aspx

Example:

<healthMonitoring enabled="true">
<providers>
<add name="EmailProvider"
type="System.Web.Management.SimpleMailWebEventProv ider"
to="@"
subjectPrefix="Error: "
buffer="true"
bufferMode="Notification" />
</providers>
<rules>
<add provider="EmailProvider" name="All App Events"
eventName="All Errors" />
</rules>
</healthMonitoring>

 
Reply With Quote
 
=?Utf-8?B?UEs5?=
Guest
Posts: n/a
 
      10-02-2007
Alexey - thanks for responding. Unfortunately I've checked the credentials
and the same username is being used in both situations (the working version
and the error version). I think "Aidy" may have been on to something when
he suggested that when an error fires, the network connections are not in a
suitable condition to send email -- I'm just not sure how to remedy that.

It doesn't look like anyone else has any answers so far unfortunately.

--
PK9


"Alexey Smirnov" wrote:

> On Oct 2, 10:53 pm, PK9 <(E-Mail Removed)> wrote:
> > I get this problem any time an error is thrown within the application and the
> > Application_Error event handler is called. I have a site in production, and
> > to test the error handling in production, I have created a button with an
> > event handler that when clicked will throw an exception. I have tested it by
> > adding "throw new ApplicationException("This is a test exception being
> > thrown...");" and by throwing a named exception "throw new
> > DivideByZeroException();".
> >
> > Both correctly go to the Application_Error event handler, but the email
> > portion fails everytime.
> >
> > I'm at a loss.
> >
> > Not sure which "default credentials" you're referring to. I have the
> > following settings specified in web.config, but I've tried to add additional
> > information such as username and password to the web.config as well (with no
> > luck):
> > web.config
> > ------------------------------------------------------------
> > <system.net>
> > <mailSettings>
> > <smtp>
> > <network host="[mynetworkHost]" port="25" />
> > </smtp>
> > </mailSettings>
> > </system.net>
> >

>
> Well, the code looks correct but I'm not sure what exactly that error
> message means. I would try to simplify Application_Error to send just
> a test email to see if it helps and I check if other part of code
> where email is working has the same identity as NT AUTHORITY\NETWORK
> SERVICE (it stays in exception)
>
> Response.Write(Environment.UserName);
>
> Maybe you use an impersonation there?
>
> BTW, check ASP.NET Health Monitoring, it can do the same using
> standard web.config configuration.
> http://msdn2.microsoft.com/en-us/library/ms178701.aspx
>
> Example:
>
> <healthMonitoring enabled="true">
> <providers>
> <add name="EmailProvider"
> type="System.Web.Management.SimpleMailWebEventProv ider"
> to="@"
> subjectPrefix="Error: "
> buffer="true"
> bufferMode="Notification" />
> </providers>
> <rules>
> <add provider="EmailProvider" name="All App Events"
> eventName="All Errors" />
> </rules>
> </healthMonitoring>
>
>

 
Reply With Quote
 
Aidy
Guest
Posts: n/a
 
      10-03-2007
I'd probably start with some basic debugging issues like attempting to send
e-mail from other events in the global.asax like application start or
session start etc to see if it is sending mail from a certain event (the
error one) or all events.

"PK9" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thank you for your response. So how would one handle this? I assume that
> catching errors in the global.asax and emailing a sys admin is typical
> practice. You may be correct that the network connects are not in a
> suitable
> condition to send email -- but I'm not sure how to remedy that.
>
> Just for further information, this happens regardless of the type of error
> thrown (i.e. dividebyzeroexception, custom applicationexception, etc).
> THe
> email functionality will simply not work from the Application_Error event
> handler.
>
> Any ideas on how to remedy this?
> --
> PK9
>
>
> "Aidy" wrote:
>
>> The actual problem is probably the code that is throwing the exception
>> not
>> tidying up its network connects, mail client etc, so that when the error
>> event in your global.asax fires your network connections are not in a
>> suitable condition to send e-mail. This will be doubly so if it is
>> e-mailing that is throwing your original exception.
>>
>> "PK9" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >I have an ASP.NET 2.0 Web Application. I am trying to send an email to
>> > myself from Global.asax in the Application_Error event handler. I have
>> > been
>> > able to successfully send emails from the rest of the application using
>> > the
>> > exact same logic. For some reason, it does not work in global.asax. I
>> > get a
>> > "Failure Sending Mail" error message with a more detailed explanation
>> > of
>> > "Unable to read data from the transport connection: An existing
>> > connection
>> > was forcibly closed by the remote host.". This is in a hosted
>> > environment,
>> > and it IS working for other pages in the app (making me think I need to
>> > do
>> > something different in the Application_Error function.
>> >
>> > I am using the System.Net.Mail class. Below is my code and the
>> > detailed
>> > error (notice I have some general information stored in web.config
>> > which
>> > is
>> > pasted below as well):
>> >
>> > GLOBAL.ASAX
>> > --------------------------------------------------------
>> > void Application_Error(object sender, EventArgs e)
>> > {
>> > // Code that runs when an unhandled error occurs
>> > Exception ex = Server.GetLastError();
>> > string strEmailAddressTo = "(E-Mail Removed)";
>> > string strEmailAddressFrom = "(E-Mail Removed)";
>> >
>> >
>> > string ErrorMessage = ex.Message +
>> > "\nSOURCE: " + ex.Source +
>> > "\nFORM: " + Request.Form.ToString() +
>> > "\nQUERYSTRING: " +
>> > Request.QueryString.ToString() +
>> > "\nTARGETSITE: " + ex.TargetSite +
>> > "\nSTACKTRACE: " + ex.StackTrace;
>> >
>> > //1) Create the mail message instance
>> > MailMessage mm = new MailMessage(strEmailAddressFrom,
>> > strEmailAddressTo);
>> >
>> > //2) Assign the Mail Message's Properties
>> > // Subject
>> > mm.Subject = "ERROR OCCURRED";
>> >
>> > //3) Body
>> > mm.Body = ErrorMessage.ToString();
>> > mm.IsBodyHtml = false;
>> >
>> > //4) Create the SMTP Client Object
>> > SmtpClient smtp = new SmtpClient();
>> >
>> >
>> > //5) Send the mail message (will use the web.config settings)
>> > smtp.Send(mm);
>> > }
>> >
>> >
>> > web.config
>> > ------------------------------------------------------------
>> > <system.net>
>> > <mailSettings>
>> > <smtp>
>> > <network host="[mynetworkHost]" port="25" />
>> > </smtp>
>> > </mailSettings>
>> > </system.net>
>> >
>> > DETAILED ERROR MESSAGE:
>> > -------------------------------------------------------------
>> > <ExceptionInformation><AdditionalInformationProper ty
>> > ExceptionManager.MachineName="XXXXX"
>> > ExceptionManager.TimeStamp="10/2/2007
>> > 1:32:25 AM"
>> > ExceptionManager.FullName="Microsoft.ApplicationBl ocks.ExceptionManagement,
>> > Version=1.0.2830.35366, Culture=neutral, PublicKeyToken=null"
>> > ExceptionManager.AppDomainName="/LM/w3svc/814863/root-11-128357766911129800"
>> > ExceptionManager.ThreadIdentity="" ExceptionManager.WindowsIdentity="NT
>> > AUTHORITY\NETWORK SERVICE" /><Exception
>> > ExceptionType="System.Net.Mail.SmtpException"
>> > StatusCode="GeneralFailure"
>> > Message="Failure sending mail."
>> > Data="System.Collections.ListDictionaryInternal" TargetSite="Void
>> > Send(System.Net.Mail.MailMessage)" Source="System"><StackTrace> at
>> > System.Net.Mail.SmtpClient.Send(MailMessage message)
>> > in \\[webpath]\web\Global.asax:line 101</StackTrace><Exception
>> > ExceptionType="System.IO.IOException" Message="Unable to read data from
>> > the
>> > transport connection: An existing connection was forcibly closed by the
>> > remote host." Data="System.Collections.ListDictionaryInternal"
>> > TargetSite="Int32 Read(Byte[], Int32, Int32)"
>> > Source="System"><StackTrace>
>> > at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
>> > Int32
>> > size)
>> > at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32
>> > count)
>> > at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset,
>> > Int32
>> > count)
>> > at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(S mtpReplyReader
>> > caller, Boolean oneLine)
>> > at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(Sm tpReplyReader
>> > caller)
>> > at System.Net.Mail.SmtpReplyReader.ReadLine()
>> > at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String&
>> > response)
>> > at System.Net.Mail.DataStopCommand.Send(SmtpConnectio n conn)
>> > at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs
>> > args)
>> > at System.Net.ClosableStream.Close()
>> > at System.Net.Mail.MailWriter.Close()
>> > at System.Net.Mail.SmtpClient.Send(MailMessage
>> > message)</StackTrace><Exception
>> > ExceptionType="System.Net.Sockets.SocketException" ErrorCode="10054"
>> > SocketErrorCode="ConnectionReset" NativeErrorCode="10054" Message="An
>> > existing connection was forcibly closed by the remote host"
>> > Data="System.Collections.ListDictionaryInternal" TargetSite="Int32
>> > Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)"
>> > Source="System"><StackTrace> at
>> > System.Net.Sockets.Socket.Receive(Byte[]
>> > buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
>> > at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
>> > Int32
>> > size)</StackTrace></Exception></Exception></Exception></ExceptionInformation>
>> >
>> >
>> > --
>> > PK9
>> >

>>
>>
>>



 
Reply With Quote
 
Dan Ribar
Guest
Posts: n/a
 
      09-02-2008

Greetings,

I am in the same place as you were. I moved to a Microsoft Exchange server that required credentials (mailobj.credentials) and it all stopped working.

I was thinking it had something to do with global.asax not wanting to do the authentication with the mail server (or something similar).


Did you get it figured out?

Thanks!

Dan
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Global.asax Application_Error not working? Ron Weldy ASP .Net 4 06-04-2005 08:09 PM
Re: ASP.NET looping in the Application_Error handler Phil Winstanley [Microsoft MVP ASP.NET] ASP .Net 0 06-21-2004 03:09 PM
ASP.NET looping in the Application_Error handler =?Utf-8?B?TWljaGFlbA==?= ASP .Net 0 06-21-2004 02:37 PM
ASP.NET Looping in Application_Error handler =?Utf-8?B?TWljaGFlbA==?= ASP .Net 0 06-16-2004 02:25 PM
Multithreading in Application_Error event handler? ram ASP .Net 1 01-14-2004 08:21 PM



Advertisments