title: System.Net.Mail.SmtpException: Failure sending mail ---> Unable to write data to the transport connection: An existing connection was forcibly closed
body: <p>I'm using windows server R2, .Net 4.5, WCF rest services and SendGrid to send email. I get the following exception when trying to send emails having multiple attachments. </p>
  
  <p>My Total attachments size is always less than 25MB and maximum of 5 attachments for an email. I get the below exception when attaching a one file with 24MB or multiple files having 100KB. I'm receiving this is totally random. Sometime it goes, most of the time I get this exception</p>
  
  <pre><code>System.Net.Mail.SmtpException: Failure sending mail. ---&gt; System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---&gt; System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
     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.Mime.EightBitStream.Write(Byte[] buffer, Int32 offset, Int32 count)
     at System.Net.Mime.BaseWriter.Flush(MultiAsyncResult multiResult)
     at System.Net.Mime.MimeWriter.Close()
     at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer, Boolean allowUnicode)
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
     --- End of inner exception stack trace ---
     at System.Net.Mail.SmtpClient.Send(MailMessage message)
  </code></pre>
  
  <p>I found this <a href="https://github.com/sendgrid/sendgrid-csharp/issues/851" rel="nofollow noreferrer">https://github.com/sendgrid/sendgrid-csharp/issues/851</a> but it does not gives a solution. I'm not even sure that my exception is related to SendGrid.</p>