locked
Failure sending mail Error in SMTP email send

    Question

  • Hi All,

    I have using the below code in window service for sending email to support team. Initially it's working fine but after 4 months later I received the following error for this function.

    This error not received frequently. Occasionally I received this error message. For example if we send 100 email per day then I receive this error for minimum 10 email sending

    Error Message:
    ***********
    Failure sending mail.
    System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.Mail.SmtpConnection.Flush() at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message)

     

    My Code:
    *******
    Dim mail As New MailMessage()
    Dim i As Integer

     Select Case sPriority.ToUpper
                    Case "NORMAL"
                        mail.Priority = MailPriority.Normal
                    Case "HIGH"
                        mail.Priority = MailPriority.High
                    Case "LOW"
                        mail.Priority = MailPriority.Low
            End Select

            mail.From = New MailAddress(sEmail_Address_From)
            mail.To.Add(sEmail_Address_To)
            If sEmail_Address_CC <> "" Then
      mail.CC.Add(sEmail_Address_CC)
     End If
            If sEmail_Address_BCC <> "" Then
                    mail.Bcc.Add(sEmail_Address_BCC)
     End If
            mail.Subject = sEmail_Subject
            mail.Body = sEmail_Body ' "this is the body content of the email."
            mail.IsBodyHtml = bIsBodyHtml
            If Not (sEmail_Attachment Is Nothing) Then
            For i = 0 To sEmail_Attachment.Length - 1
             If sEmail_Attachment(i) <> "" Then
                            mail.Attachments.Add(New Attachment(sEmail_Attachment(i)))
                        End If
                    Next
            End If
            Dim smtp As New SmtpClient("SMTP Server IP")
            smtp.Send(mail)


    Thursday, April 28, 2011 2:43 AM

All replies

  • Hi Sundar.Mannai,

    I have generated your case scenario and found some cause of it... there is no solution to it...

    Unable to read data from the transport connection: net_io_connectionclosed.

    Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
    [IOException: Unable to read data from the transport connection: net_io_connectionclosed.]
    System.Net.Mail.SmtpReplyStreamFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) +223
    System.Net.Mail.SmtpReplyStreamFactory.ReadLines(SmtpReplyStream caller, Boolean oneLine) +303
    System.Net.Mail.SmtpReplyStreamFactory.ReadLine(SmtpReplyStream caller) +53
    System.Net.Mail.SmtpReplyStream.ReadLine() +47
    System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) +1027
    System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) +287
    System.Net.Mail.SmtpClient.GetConnection() +56
    System.Net.Mail.SmtpClient.Send(MailMessage message) +1664

    [SmtpException: Failure sending mail.]
    System.Net.Mail.SmtpClient.Send(MailMessage message) +2171
    System.Web.UI.Util.SendMail(MailMessage message) +54
    System.Web.UI.WebControls.PasswordRecovery.SendMail(String to, String userName, String password) +415
    System.Web.UI.WebControls.PasswordRecovery.AttemptSendPasswordQuestionView() +603
    System.Web.UI.WebControls.PasswordRecovery.AttemptSendPassword() +93
    System.Web.UI.WebControls.PasswordRecovery.OnBubbleEvent(Object source, EventArgs e) +135
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
    System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +107
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +163
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72

    What this error means is that System.net.mail was unable to find the smtp server.

    The answer will vary depending on whether you have a fixed IP or a dynamic IP but, basically, you need to assign a valid IP to your smtp server.  In your scenario you are using a dynamic IP so might be there are cases when the target doesnot have an SMTP installed on it or might be the port of target is closed for receiving the message or might be a bla bla cause... in dynamic ips this is generally a case... as you are using a dynamic ip as mentioned by you as "SMTP Server IP"

    With fixed IP's this is relatively straightforward.
    With dynamic IP's it takes a bit of tweaking.

    Otherwise your code is perfect...

    If you want another code that can help you just visit my website at www.Rajaryantechgroup.in --> click on the first page home link --> click on www.RajaryanTech.in --> Goto Visual Studio section and there you will find a link called sending emails using Csharp Windows application... same is for ASP.NET tooo...

    Dont forget to mark as answer it you understood it....

    Thanks


    Roozan Parvez Bharucha MCT, MCITP (SQL Server 2008, Windows 7 Admin), MCTS (SQL Server 2008, Windows 7 Config, .NET Frmwk 3.5, .NET Frmwk 4.0 (Windows)), MCPD Enterprize App (Frmwk 3.5), MCPD Windows (Frmwk 4.0) CEO, RajAryanTech
    • Proposed as answer by Rex Honour Sunday, May 01, 2011 10:01 AM
    Sunday, May 01, 2011 10:01 AM
  • Hi Sundar.Mannai,

    I have generated your case scenario and found some cause of it... there is no solution to it...

    Unable to read data from the transport connection: net_io_connectionclosed.

    Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
    [IOException: Unable to read data from the transport connection: net_io_connectionclosed.]
    System.Net.Mail.SmtpReplyStreamFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) +223
    System.Net.Mail.SmtpReplyStreamFactory.ReadLines(SmtpReplyStream caller, Boolean oneLine) +303
    System.Net.Mail.SmtpReplyStreamFactory.ReadLine(SmtpReplyStream caller) +53
    System.Net.Mail.SmtpReplyStream.ReadLine() +47
    System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) +1027
    System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) +287
    System.Net.Mail.SmtpClient.GetConnection() +56
    System.Net.Mail.SmtpClient.Send(MailMessage message) +1664

    [SmtpException: Failure sending mail.]
    System.Net.Mail.SmtpClient.Send(MailMessage message) +2171
    System.Web.UI.Util.SendMail(MailMessage message) +54
    System.Web.UI.WebControls.PasswordRecovery.SendMail(String to, String userName, String password) +415
    System.Web.UI.WebControls.PasswordRecovery.AttemptSendPasswordQuestionView() +603
    System.Web.UI.WebControls.PasswordRecovery.AttemptSendPassword() +93
    System.Web.UI.WebControls.PasswordRecovery.OnBubbleEvent(Object source, EventArgs e) +135
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
    System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +107
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +163
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72

    What this error means is that System.net.mail was unable to find the smtp server.

    The answer will vary depending on whether you have a fixed IP or a dynamic IP but, basically, you need to assign a valid IP to your smtp server. In your scenario you are using a dynamic IP so might be there are cases when the target doesnot have an SMTP installed on it or might be the port of target is closed for receiving the message or might be a bla bla cause... in dynamic ips this is generally a case... as you are using a dynamic ip as mentioned by you as "SMTP Server IP"

    With fixed IP's this is relatively straightforward.
    With dynamic IP's it takes a bit of tweaking.

    Otherwise your code is perfect...

    If you want another code that can help you just visit my website at www.Rajaryantechgroup.in --> click on the first page home link --> click on www.RajaryanTech.in --> Goto Visual Studio section and there you will find a link called sending emails using Csharp Windows application... same is for ASP.NET tooo...

    Dont forget to mark as answer it you understood it....

    Thanks


    Roozan Parvez Bharucha MCT, MCITP (SQL Server 2008, Windows 7 Admin), MCTS (SQL Server 2008, Windows 7 Config, .NET Frmwk 3.5, .NET Frmwk 4.0 (Windows)), MCPD Enterprize App (Frmwk 3.5), MCPD Windows (Frmwk 4.0) CEO, RajAryanTech
    • Proposed as answer by Rex Honour Sunday, May 01, 2011 10:10 AM
    Sunday, May 01, 2011 10:10 AM