1

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);
    }
}
rashfmnb
  • 9,959
  • 4
  • 33
  • 44
rdbradshaw
  • 223
  • 1
  • 12
  • Maybe you are sending items too quickly one after the other, try wait a bit between each sending. – aybe Apr 15 '16 at 19:48
  • The timing changes each time. Could take several minutes for the workbook to create before the mail tries to send and it still fails. – rdbradshaw Apr 15 '16 at 19:49
  • maybe check out similar questions: http://stackoverflow.com/q/17497154/4498937 and http://stackoverflow.com/q/20228644/4498937 – terbubbs Apr 15 '16 at 19:49
  • This seems most relevant: http://stackoverflow.com/questions/24372824/vba-loop-to-send-emails-with-attachments-also-includes-all-previous-iterations The thing I'd key into is the "loop sending with attachments" that you put in the subject. – Scott Mermelstein Apr 15 '16 at 19:51
  • @terbubbs I saw both of these before I posted, each of these is for a single email send that fails. Mine is sending fine for the first two emails, but fails on the third each time. – rdbradshaw Apr 15 '16 at 19:53

1 Answers1

1

Most likely you are hitting smtp server antispam filter when you sending messages one right after another. Try to space them apart in time and make 3 attempts for each send. Obviously you should not stop if any one send is failed, you should go through all 12.

Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20