我已经被这个问题困扰了好几个月了,这让我抓狂。
我有一个用 C# (.NET 4.5) 编写的 Windows 服务,它基本上使用 Outlook 帐户发送电子邮件(我认为这是一个 Office365 服务)。我知道“凭据顺序”问题,这不会影响我(许多电子邮件都能正确发送)。
该服务正确启动并开始发送电子邮件。有时,当数量太多时,我会收到服务器错误等待,服务会等待几分钟并自行完美地继续。
在这些情况下我得到错误 A:
System.Net.Mail.SmtpException: The operation has timed out.
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Cobranzas.WinService.Email.SendEmail(String subject, String body, String mailTo, String attPath)
at Cobranzas.WinService.CobranzasEmailService.SendEmails(IEnumerable`1 toSend, RepositoryEf emailRepo)
问题:有时,我找不到模式,它每隔几天就会发生一次,它会出现超时错误,并且永远不会恢复(重新启动服务即可立即修复)。所有后续发送尝试都会失败并出现相同的错误。在这种情况下,我得到了错误 A 和错误 A 的混合:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The operation has timed out.
at System.Net.ConnectionPool.Get(Object owningObject, Int32 result, Boolean& continueLoop, WaitHandle[]& waitHandles)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Cobranzas.WinService.Email.SendEmail(String subject, String body, String mailTo, String attPath)
at Cobranzas.WinService.CobranzasEmailService.SendEmails(IEnumerable`1 toSend, RepositoryEf emailRepo)
我的服务的逻辑如下:我有一个计时器,每 5 分钟迭代一次要发送的大量电子邮件,并为每个执行
Thread.Sleep(2000);
try
{
emailService.SendEmail(asunto, nuevoCuerpo, mail.Email, mail.AlertMessage.Attach);
}
catch (Exception ex)
{
if (ex is System.Net.Mail.SmtpException)
{
Thread.Sleep(20000); // Lo hacemos esperar 20 segundos
}
}
SendEmail方法是:
var mailMessage = new MailMessage();
mailMessage.To.Add(mailTo);
mailMessage.Subject = subject;
mailMessage.Body = WebUtility.HtmlDecode(body);
mailMessage.IsBodyHtml = true;
mailMessage.From = new MailAddress(emailFromAddress, emailFromName);
mailMessage.Headers.Add("Content-type", "text/html; charset=iso-8859-1");
// Attachment
if (attPath != null)
{
var data = new Attachment(attPath, MediaTypeNames.Application.Octet);
mailMessage.Attachments.Add(data);
}
var cred =
new NetworkCredential(emailFromAddress, emailFromPassword);
using (var mailClient =
new SmtpClient(emailSmtpClient, emailSmtpPort)
{
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Timeout = 20000,
Credentials = cred
})
{
mailClient.Send(mailMessage);
}
foreach (Attachment attachment in mailMessage.Attachments)
{
attachment.Dispose();
}
使用 SmtpClient 和附件处理是新的,我们添加它们试图解决这个问题。行为没有改变。
T̶h̶e̶ ̶T̶h̶r̶e̶a̶d̶.̶S̶l̶e̶e̶p̶ ̶a̶f̶t̶e̶r̶ ̶t̶h̶e̶ ̶t̶i̶m̶e̶o̶u̶t̶ ̶i̶s̶ ̶n̶e̶w ̶ ̶a̶n̶d̶ ̶u̶n̶t̶e̶s̶t̶e̶d̶̶y̶e̶t̶。
鉴于重新启动服务可以修复它,我怀疑某些内容没有被适当关闭/清理,但我已经检查过,但找不到它可能是什么。我发现this http://connect.microsoft.com/VisualStudio/feedback/details/146711/smtp-never-sends-the-quit-command链接不久前,但看起来很旧。
任何帮助深表感谢!
[进步]
我已经测试了超时后 20 英寸的等待,但没有任何结果,它仍然以同样的方式失败。有什么想法吗?我们真的对此感到困惑。