gpt4 book ai didi

c# - 服务器超时后,c# 中的电子邮件发送服务无法恢复

转载 作者:可可西里 更新时间:2023-11-01 08:45:01 25 4
gpt4 key购买 nike

这个问题困扰我好几个月了,快把我逼疯了。我有一个用 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 和:

 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̶h̶e̶e̶a̶d̶.s̶s̶e̶e̶e̶p̶p̶e̶p̶

考虑到重新启动服务可以修复它,我怀疑有什么东西没有被适本地关闭/清理,但我已经检查过但找不到它可能是什么。我找到了 this前一阵子链接,但它看起来很旧。

非常感谢任何帮助!

[进展]

我已经在超时后测试了 20"等待,但什么也没有,它仍然以同样的方式失败。有什么想法吗?我们真的被这个问题难住了。

最佳答案

我也有同样的问题。我在这里找到了解决方案:

发出两个以上并发 WebRequest 时出现 System.Net.WebException http://www.wadewegner.com/2007/08/systemnetwebexception-when-issuing-more-than-two-concurrent-webrequests/

基本上,我更改了允许同时连接到单个外部服务器的最大连接数。默认情况下,根据该文档,它是 2。虽然它有一个超时(它会等待一段时间直到一个被释放然后继续),但有时,它是不够的并且挂起。

<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="100" />
</connectionManagement>
</system.net>
</configuration>

我无法找到如何在第一次超时后恢复,但希望这会在一段时间内解决“超时”错误。

关于c# - 服务器超时后,c# 中的电子邮件发送服务无法恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24633809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com