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>
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>