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);
}