none
SMTPClient.Send fails with error " System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host" RRS feed

  • Question

  • C# service fails to send email with multiple attachments. The failure occurs when sending >3 attachments. This failure occurs even if the total attachment size under 100KB.

    Troubleshooting notes:
    - Targets Framework is 4.0.
    - The other threads for "existing connection was forcibly closed" are not relevant to this issue because I'm using System.Net.Mail class to send emails.
    - Sending email to local SMTP service. And local SMTP service is able to relay emails without issues
    - Captured System.Net class trace. Can provide entire log, if required.

    App log shows following error:
    System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from 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.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
       --- End of inner exception stack trace ---
       at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
       at System.Net.Mime.MimePart.Send(BaseWriter writer)
       at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer)
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       --- End of inner exception stack trace ---
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at Corp.Framework.EmailManager.SendMail(StringCollection toAddresses, Attachment[] attachments)ection toAddresses, Attachment[] attachments)


    System.Net* Trace captures following errors:

    System.Net Verbose: 0 : [4580] Exiting ConnectStream#2629989::Read()     -> 1453#1453
    System.Net Verbose: 0 : [4580] ConnectStream#2629989::Read()
    System.Net.Sockets Verbose: 0 : [4580] Socket#57782509::Receive()
    System.Net.Sockets Error: 0 : [4580] Exception in the Socket#57782509::Receive - An existing connection was forcibly closed by the remote host
    System.Net.Sockets Verbose: 0 : [4580] Exiting Socket#57782509::Receive()     -> 0#0
    System.Net.Sockets Verbose: 0 : [4580] Socket#57782509::Dispose()
    System.Net Error: 0 : [4580] Exception in the HttpWebRequest#9035793:: - The underlying connection was closed: An unexpected error occurred on a receive.
    System.Net Error: 0 : [4580] Exception in the SmtpClient#12046712::Send - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
    System.Net Error: 0 : [4580]    at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
       at System.Net.Mime.MimePart.Send(BaseWriter writer)
       at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer)
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
    System.Net.Sockets Verbose: 0 : [4580] Socket#42706818::Dispose()
    System.Net Verbose: 0 : [4580] SmtpPooledStream::Dispose #54415608
    System.Net Verbose: 0 : [4580] Exiting SmtpPooledStream::Dispose #54415608
    System.Net Verbose: 0 : [4580] Exiting SmtpClient#12046712::Send()

    Sample code:

    MailMessage mail = new MailMessage();
    mail.Subject = _MessageSubject;
    mail.Body = _MessageBody;
    mail.IsBodyHtml = false;
    mail.BodyEncoding = System.Text.Encoding.UTF8;
    mail.Priority = _Priority;

    foreach (Object a in _Attachments)
    {
        mail.Attachments.Add((Attachment)a);
    }

    foreach(string to in toAddresses)
    {
            mail.To.Add(to);
    }

    mail.From = new MailAddress(_FromAddress);
    SmtpClient smtpClient = new SmtpClient(_SMTPServer);

    smtpClient.Host = _SMTPServer;
    smtpClient.EnableSsl = _IsSSLEnabled;
    smtpClient.Port = _SMTPPort;

    try
    {
           smtpClient.Send(mail);
    }



    • Edited by tlegtcdi9 Wednesday, December 11, 2013 3:36 PM
    Wednesday, December 11, 2013 3:31 PM

Answers

  • The StackTrace indicates the issues is local on the client computer.  the Stacktrace is backwads where the top of the tace Stack is the latest info and the bottom of the trace stack the earliest info.  The email started to send and opened up a read stream to the attachment and a writer to the network.  The read stream failed, not the network write.  The file could be corrupted or contains contents/length that is being blocked by a firewall.


    jdweng

    Wednesday, December 18, 2013 1:00 PM

All replies

  • Are you surre the problem is with multiple attachements?  SMTP you havve to dispose the client if you are sending more than one email.  Yo uwill always get an error similar with the one you are gettting if you send two emails without disposing.  the only way of disposing the SMTPClient is to put it into a class and making the class disposible.

    An existing connection was forcibly closed by the remote host.


    jdweng

    Wednesday, December 11, 2013 3:42 PM
  • Definitely issue with multiple attachments. The error occurs when sending >3 attachments.

    This issue occurs when sending one email (assuming multiple recipients are considered one email.) So I'm not sure if this is related to making the class disposable. I will look into this anyway.

    Wednesday, December 11, 2013 3:47 PM
  • It is one email if the TO Address has all three emails combined with a semicolon between the addresses.  It is 3 emails if there is one one address for each email.

    jdweng

    Wednesday, December 11, 2013 3:51 PM
  • I think its one email with three address. Here's the code:

    foreach(string to in toAddresses)
    {
            mail.To.Add(to);
    }

    The initial post has entire email.


    Wednesday, December 11, 2013 4:06 PM
  • Try sending a test email with one To address and no attachemnts and see if the problem still occurs.  You may have a firewall blocking the SMTP port number. there are lots of reasons for the forceable close coneciton exception.  I'm trying to make sure we are attacking the right problem.

    jdweng

    Wednesday, December 11, 2013 4:37 PM
  • Email with <3 attachment succeeds. Able to reproduce it after uninstalling Antivirus and Firewall software.

    When is this message displayed?
    System.Net.Sockets Error: 0 : [4580] Exception in the Socket#57782509::Receive - An existing connection was forcibly closed by the remote host

    Rest of the trace:
    System.Net Verbose: 0 : [4580] 00000580 : 00 00 00 00 00 00 45 78-70 6F 72 74 41 6C 6C 44 : ......ExportAllD
    System.Net Verbose: 0 : [4580] 00000590 : 61 74 61 2E 63 73 76 50-4B 05 06 00 00 00 00 01 : ata.csvPK.......
    System.Net Verbose: 0 : [4580] 000005A0 : 00 01 00 3F 00 00 00 58-05 00 00 00 00          : ...?...X.....
    System.Net Verbose: 0 : [4580] Exiting ConnectStream#2629989::Read()  -> 1453#1453
    System.Net Verbose: 0 : [4580] ConnectStream#2629989::Read()
    System.Net.Sockets Verbose: 0 : [4580] Socket#57782509::Receive()
    System.Net.Sockets Error: 0 : [4580] Exception in the Socket#57782509::Receive - An existing connection was forcibly closed by the remote host
    System.Net.Sockets Verbose: 0 : [4580] Exiting Socket#57782509::Receive()  -> 0#0
    System.Net.Sockets Verbose: 0 : [4580] Socket#57782509::Dispose()
    System.Net Error: 0 : [4580] Exception in the HttpWebRequest#9035793:: - The underlying connection was closed: An unexpected error occurred on a receive.
    System.Net Error: 0 : [4580] Exception in the SmtpClient#12046712::Send - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
    System.Net Error: 0 : [4580]    at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
       at System.Net.Mime.MimePart.Send(BaseWriter writer)
       at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer)
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
    System.Net.Sockets Verbose: 0 : [4580] Socket#42706818::Dispose()
    System.Net Verbose: 0 : [4580] SmtpPooledStream::Dispose #54415608
    System.Net Verbose: 0 : [4580] Exiting SmtpPooledStream::Dispose #54415608
    System.Net Verbose: 0 : [4580] Exiting SmtpClient#12046712::Send()

    Thursday, December 12, 2013 6:47 PM
  • Hi Tlegtcdi,

    You need to pass login credentials:

    SmtpClient smtpClient = new SmtpClient(_SMTPServer);

                smtpClient.Host = _SMTPServer;

                smtpClient.EnableSsl = _IsSSLEnabled;

                smtpClient.Port = _SMTPPort;

                smtpClient.Credentials = new NetworkCredential(emailaddress, password);

    Hope useful to you.

    Reagards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 16, 2013 6:13 AM
    Moderator
  • Passing credentials does not resolve this issue. Are there any other troubleshooting steps?

    Wednesday, December 18, 2013 12:16 PM
  • When you are refering to three attachments do you mean email addresses or actual document attachments?

    Is it possible the 3rd attachment (email adress or document attachment) is causing the error?  For some reason the 3rd item may be the cause of the issue. 


    jdweng

    Wednesday, December 18, 2013 12:21 PM
  • I was referring to an actual attachment. These attachments are zipped files. The actual email attempts to send 7 attachments.  This failure is reproducible with 3 or more attachments. 

    We are verifying the firewall/antivirus uninstallation. AV/Firewall wasn't uninstalled during the test.

    thanks,

    tlegtcdi9

    Wednesday, December 18, 2013 12:30 PM
  • This isn't a VS or Net Library issue.  I think you are exceeding a limation in you mail server.  Try manually sending the email from the server and see if it goes through.  The transport network layer for SMTP is TCP.  Some servers (or internet gateways) will limit TCP messages to 10M to 20M which may be performed by a firewall utility.  The blocking firewall may not be on you PC but at your companies internet gateway.  Check with your MIS people.


    jdweng

    Wednesday, December 18, 2013 12:35 PM
  • Are there group policy or registry settings that block sendmail on Windows 2008 R2 VMware VM?

    I also started the troubleshooting with that assumption. However that's not the case here.

    - Using test C# app and 3rd party app we were able to send >3 attachments.
    - I don't think its attachment size. As mentioned earlier we have sent large attachments.
    - Was able to reproduce this issue after reducing MTU size on local box
    - Sendmail fails when sending email to local SMTP service (installed same box as the service.)

    Look at the error stack. The error occurs in .Net class.
    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
       --- End of inner exception stack trace ---
       at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
       at System.Net.Mime.MimePart.Send(BaseWriter writer)
       at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer)
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       --- End of inner exception stack trace ---
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at Corp.Framework.EmailManager.SendMail(StringCollection toAddresses, Attachment[] attachments)ection toAddresses, Attachment[] attachments)


    • Edited by tlegtcdi9 Wednesday, December 18, 2013 12:46 PM
    Wednesday, December 18, 2013 12:45 PM
  • The StackTrace indicates the issues is local on the client computer.  the Stacktrace is backwads where the top of the tace Stack is the latest info and the bottom of the trace stack the earliest info.  The email started to send and opened up a read stream to the attachment and a writer to the network.  The read stream failed, not the network write.  The file could be corrupted or contains contents/length that is being blocked by a firewall.


    jdweng

    Wednesday, December 18, 2013 1:00 PM