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

Discussion in 'ASP .Net' started by =?Utf-8?B?UEs5?=, Oct 2, 2007.

  1. 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 = "";
    string strEmailAddressFrom = "";


    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>


    --
    PK9
     
    =?Utf-8?B?UEs5?=, Oct 2, 2007
    #1
    1. Advertising

  2. =?Utf-8?B?UEs5?=

    Aidy Guest

    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" <> wrote in message
    news:...
    >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 = "";
    > string strEmailAddressFrom = "";
    >
    >
    > 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>
    >
    >
    > --
    > PK9
    >
     
    Aidy, Oct 2, 2007
    #2
    1. Advertising

  3. On Oct 2, 5:33 pm, PK9 <> 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 = "";
    > string strEmailAddressFrom = "";
    >
    > 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>
    >
    > --
    > PK9


    1. How do you get that error message?
    2. Did you tried to specify default credential in mailSettings?
     
    Alexey Smirnov, Oct 2, 2007
    #3
  4. Re: SMTP Email Not Working from Application_Error handler in ASP.N

    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" <> wrote in message
    > news:...
    > >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 = "";
    > > string strEmailAddressFrom = "";
    > >
    > >
    > > 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>
    > >
    > >
    > > --
    > > PK9
    > >

    >
    >
    >
     
    =?Utf-8?B?UEs5?=, Oct 2, 2007
    #4
  5. Re: SMTP Email Not Working from Application_Error handler in ASP.N

    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 <> 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 = "";
    > > string strEmailAddressFrom = "";
    > >
    > > 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>
    > >
    > > --
    > > PK9

    >
    > 1. How do you get that error message?
    > 2. Did you tried to specify default credential in mailSettings?
    >
    >
     
    =?Utf-8?B?UEs5?=, Oct 2, 2007
    #5
  6. Re: SMTP Email Not Working from Application_Error handler in ASP.N

    On Oct 2, 10:53 pm, PK9 <> 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.SimpleMailWebEventProvider"
    to="@"
    subjectPrefix="Error: "
    buffer="true"
    bufferMode="Notification" />
    </providers>
    <rules>
    <add provider="EmailProvider" name="All App Events"
    eventName="All Errors" />
    </rules>
    </healthMonitoring>
     
    Alexey Smirnov, Oct 2, 2007
    #6
  7. Re: SMTP Email Not Working from Application_Error handler in ASP.N

    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 <> 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.SimpleMailWebEventProvider"
    > to="@"
    > subjectPrefix="Error: "
    > buffer="true"
    > bufferMode="Notification" />
    > </providers>
    > <rules>
    > <add provider="EmailProvider" name="All App Events"
    > eventName="All Errors" />
    > </rules>
    > </healthMonitoring>
    >
    >
     
    =?Utf-8?B?UEs5?=, Oct 3, 2007
    #7
  8. =?Utf-8?B?UEs5?=

    Aidy Guest

    Re: SMTP Email Not Working from Application_Error handler in ASP.N

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    >> news:...
    >> >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 = "";
    >> > string strEmailAddressFrom = "";
    >> >
    >> >
    >> > 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>
    >> >
    >> >
    >> > --
    >> > PK9
    >> >

    >>
    >>
    >>
     
    Aidy, Oct 3, 2007
    #8
  9. =?Utf-8?B?UEs5?=

    Dan Ribar Guest

    Did you get this worked out?

    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
     
    Dan Ribar, Sep 2, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ram
    Replies:
    1
    Views:
    489
    Sami Vaaraniemi
    Jan 14, 2004
  2. =?Utf-8?B?TWljaGFlbA==?=

    ASP.NET Looping in Application_Error handler

    =?Utf-8?B?TWljaGFlbA==?=, Jun 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    543
    =?Utf-8?B?TWljaGFlbA==?=
    Jun 16, 2004
  3. =?Utf-8?B?TWljaGFlbA==?=

    ASP.NET looping in the Application_Error handler

    =?Utf-8?B?TWljaGFlbA==?=, Jun 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    329
    =?Utf-8?B?TWljaGFlbA==?=
    Jun 21, 2004
  4. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: ASP.NET looping in the Application_Error handler

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    528
    Phil Winstanley [Microsoft MVP ASP.NET]
    Jun 21, 2004
  5. Ron Weldy
    Replies:
    4
    Views:
    7,149
    Ron Weldy
    Jun 4, 2005
Loading...

Share This Page