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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 address removed)";
string strEmailAddressFrom = "(e-mail address 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><AdditionalInformationProperty
ExceptionManager.MachineName="XXXXX" ExceptionManager.TimeStamp="10/2/2007
1:32:25 AM"
ExceptionManager.FullName="Microsoft.ApplicationBlocks.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(SmtpReplyReader
caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
at System.Net.Mail.DataStopCommand.Send(SmtpConnection 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>
 
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 said:
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 address removed)";
string strEmailAddressFrom = "(e-mail address 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><AdditionalInformationProperty
ExceptionManager.MachineName="XXXXX" ExceptionManager.TimeStamp="10/2/2007
1:32:25 AM"
ExceptionManager.FullName="Microsoft.ApplicationBlocks.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(SmtpReplyReader
caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader
caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String&
response)
at System.Net.Mail.DataStopCommand.Send(SmtpConnection 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>
 
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 address removed)";
string strEmailAddressFrom = "(e-mail address 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><AdditionalInformationProperty
ExceptionManager.MachineName="XXXXX" ExceptionManager.TimeStamp="10/2/2007
1:32:25 AM"
ExceptionManager.FullName="Microsoft.ApplicationBlocks.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(SmtpReplyReader
caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
at System.Net.Mail.DataStopCommand.Send(SmtpConnection 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>

1. How do you get that error message?
2. Did you tried to specify default credential in mailSettings?
 
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 said:
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 said:
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 address removed)";
string strEmailAddressFrom = "(e-mail address 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><AdditionalInformationProperty
ExceptionManager.MachineName="XXXXX" ExceptionManager.TimeStamp="10/2/2007
1:32:25 AM"
ExceptionManager.FullName="Microsoft.ApplicationBlocks.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(SmtpReplyReader
caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader
caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String&
response)
at System.Net.Mail.DataStopCommand.Send(SmtpConnection 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>
 
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 said:
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 address removed)";
string strEmailAddressFrom = "(e-mail address 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><AdditionalInformationProperty
ExceptionManager.MachineName="XXXXX" ExceptionManager.TimeStamp="10/2/2007
1:32:25 AM"
ExceptionManager.FullName="Microsoft.ApplicationBlocks.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(SmtpReplyReader
caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
at System.Net.Mail.DataStopCommand.Send(SmtpConnection 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>

1. How do you get that error message?
2. Did you tried to specify default credential in mailSettings?
 
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.SimpleMailWebEventProvider"
to="@"
subjectPrefix="Error: "
buffer="true"
bufferMode="Notification" />
</providers>
<rules>
<add provider="EmailProvider" name="All App Events"
eventName="All Errors" />
</rules>
</healthMonitoring>
 
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 said:
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.SimpleMailWebEventProvider"
to="@"
subjectPrefix="Error: "
buffer="true"
bufferMode="Notification" />
</providers>
<rules>
<add provider="EmailProvider" name="All App Events"
eventName="All Errors" />
</rules>
</healthMonitoring>
 
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 said:
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 said:
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 said:
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 address removed)";
string strEmailAddressFrom = "(e-mail address 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><AdditionalInformationProperty
ExceptionManager.MachineName="XXXXX"
ExceptionManager.TimeStamp="10/2/2007
1:32:25 AM"
ExceptionManager.FullName="Microsoft.ApplicationBlocks.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(SmtpReplyReader
caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader
caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String&
response)
at System.Net.Mail.DataStopCommand.Send(SmtpConnection 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>
 
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
(e-mail address removed)
 
Back
Top