【问题标题】:SmtpException: Failure sending mail. Loop sending with AttachmentsSmtpException: 发送邮件失败。带附件循环发送
【发布时间】:2016-08-07 22:18:14
【问题描述】:

我有一个控制台应用程序,可以将电子邮件发送到几个(目前 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("myEmailAddress@email.com", "");
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("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);
    }
}

【问题讨论】:

  • 可能是您一个接一个地发送物品太快了,请尝试在每次发送之间稍等片刻。
  • 时间每次都会改变。在邮件尝试发送之前创建工作簿可能需要几分钟时间,但仍然失败。
  • 这似乎最相关:stackoverflow.com/questions/24372824/… 我要重点关注的是您在主题中输入的“带附件的循环发送”。
  • @terbubbs 我在发布之前看到了这两个,每个都是针对失败的单个电子邮件发送。我的前两封邮件发送正常,但第三封邮件每次都发送失败。

标签: c# loops smtpclient


【解决方案1】:

当您一个接一个地发送消息时,您很可能会点击 smtp 服务器反垃圾邮件过滤器。尝试及时将它们分开,每次发送尝试 3 次。显然,如果任何一个发送失败,您不应该停止,您应该通过所有 12。

【讨论】:

  • 添加Thread.Sleep()后继续失败;到流程。
  • 例如,您的 smtp 服务器是否有限制一天内发送的电子邮件数量的政策?
  • 我已更新为每封电子邮件最多允许重试 10 次。似乎已经成功了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 2014-11-18
相关资源
最近更新 更多