System.Net.Mail.SmtpException: Failure sending mail ---> Unable to write data to the transport connection: An existing connection was forcibly closed
10:20 18 Feb 2020

I'm using Windows Server 2012 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.

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

System.Net.Mail.SmtpException: 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.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)

I found this https://github.com/sendgrid/sendgrid-csharp/issues/851 but it does not gives a solution. I'm not even sure that my exception is related to SendGrid.

c# .net wcf sendgrid