我有一个控制台应用程序,可以将电子邮件发送到几个(当前有12个)不同的电子邮件组。该过程循环进行,每次创建一个不同的excel工作簿,保存该工作簿,然后发送带有该工作簿的电子邮件。
这个过程进行了好几个月。现在,该程序将通过2封电子邮件进行处理,然后将引发异常,因为Failure sending mail.
我发现内部异常是Unable to read data from the transport connection: net_io_connectionclosed
通过2个SQL存储过程创建工作簿。即使电子邮件失败,也可以创建并打开工作簿,因此我认为这与所涉及的SQL没有任何关系。
我已经看到其他一些类似问题的SO问题,但是由于我的过程适用于2,因此失败,适用于1,适用于2,失败针对1。...我相信我的问题与简单的端口或凭据问题有所不同。
这是我之前声明SmtpClient的代码
SmtpClient client = new SmtpClient("intmail.MyDomain.net", 25);
NetworkCredential cred = new NetworkCredential("[email protected]", "");
client.Credentials = cred; // Send our account login details to the client.
client.EnableSsl = false; // Read below.
这是我在循环中创建电子邮件,附加工作簿和发送代码。
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("[email protected]");
//msg.CC.Add(new MailAddress("[email protected]"));
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);
}
}
当您接连发送邮件时,很可能是您遇到了smtp服务器反垃圾邮件过滤器。尝试将它们间隔开,并为每次发送进行3次尝试。显然,如果任何一个发送失败,您都不应停止,您应该经历所有12个。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句