I have a console application that sends emails to several (12 currently) different email groups. The process loops through and each time creates a different excel workbook, saves the workbook, then sends an email with this workbook attached.
This process ran great for several months. Now, the program will make it through 2 emails, then it will throw an exception for Failure sending mail.
I have found the inner exception is Unable to read data from the transport connection: net_io_connectionclosed
The workbooks are created via 2 SQL stored procs. Even when the email fails, the workbooks are created and can be opened so I don't think it has anything to do with the SQL involved.
I have seen several other SO issues for similar issues, but since my process works for 2, fails for 1, works for 2, fails for 1.... I believe my issue is different than a simple port or credentials issue.
Here is the code I have before loop to declare the SmtpClient
SmtpClient client = new SmtpClient("intmail.MyDomain.net", 25);
NetworkCredential cred = new NetworkCredential("myEmailAddress@email.com", "");
client.Credentials = cred; // Send our account login details to the client.
client.EnableSsl = false; // Read below.
Here is my code inside the loop for creating email, attaching workbook, and sending.
MailMessage msg = new MailMessage();
Attachment xls01 = new Attachment(filePath);
string emailTo = ClientEmail;
string[] emailTos = emailTo.Split(new char[] { ';' });
foreach (string To in emailTos)
{
msg.To.Add(To);
Console.WriteLine(To);
}
msg.From = new MailAddress("myEmailAddress@email.com");
//msg.CC.Add(new MailAddress("myEmailAddress@email.com"));
msg.Subject = ClientName + reportMonth + " " + reportYear;
msg.Body = "Attached is report for period ending on the last day of " + reportMonth + " " + reportYear + ".\r\nAlso attached are the 13 month trends.";
msg.Attachments.Add(xls01);
try
{
client.Send(msg);
}
catch (Exception ex)
{
Console.WriteLine(ex); //Should print stacktrace + details of inner exception
if (ex.InnerException != null)
{
Console.WriteLine("InnerException is: {0}", ex.InnerException);
}
}