了解 SmtpClient 如何处理重试
我正在使用 GoogleApps 帐户从我的 (mvc) 网络应用程序发送事件通知。通常一切正常。当系统被要求发送超过 75 条消息时,我会看到来自 SMTP 服务器的回复:
服务不可用,正在关闭 传输通道。服务器 响应是:4.7.0 稍后再试, 关闭连接。 (邮件) uf10sm1153163icb.17
但是,系统正在自动重试,并且我的系统最终被要求发送的任何内容(据我目前所知)都会成功。但考虑到代码如何生成和发送电子邮件,我不明白如何处理这些重试。
我想尝试减慢传输速度,希望如果异步提交发生,导致“服务不可用”情况的任何原因都能得到解决。但从代码的外观来看,它已经是因为我正在使用 Try | 。捕获块。
这是我的外观代码的相关部分:
foreach (string email in recipientEmails)
{
try
{
EmailGateway.Instance.SendEmail(email, notificationSubject, notificationMessage);
}
catch (Exception ex)
{
Logger.Instance.LogException(ex);
Logger.Instance.LogMessage("ERROR! Unsent email is to: " + email );
}
Logger.Instance.LogMessage("Sent " + notificationSubject + " to " + email);
}
这是网关代码(使用 System.Net.Mail;):
public virtual void SendEmail(string replyToAddress, string toEmailAddress, string subject, string body)
{
string destinationEmailAddress = toEmailAddress;
string fromEmailAddress = "[email protected]";
bool useSSL = "true";
MailMessage message = new MailMessage(fromEmailAddress, destinationEmailAddress, subject, body);
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = useSSL;
smtp.Send(message);
}
所以我将成功和失败都捕获到我的记录器表中。我不明白的是,如何查看同一个电子邮件地址的两者失败条件和成功条件的日志消息。这表示“重试”,虽然我对 SmtpClient(本机 .net 程序集)可以在没有显式代码要求的情况下重试并不感到惊讶,但我不明白我的外观代码是如何记录这两个条件的。
I'm working against a GoogleApps account for sending event notifications from my (mvc) web-app. Usually everything works fine. When the system is asked to send more than 75 messages or so I'm seeing replies from the SMTP server:
Service not available, closing
transmission channel. The server
response was: 4.7.0 Try again later,
closing connection. (MAIL)
uf10sm1153163icb.17
However, the system is auto-retrying and anything my system is asked to send eventually (by everything i can tell as this point) making it out. But given how the code generates and sends the emails I don't understand how these re-tries are handled.
I'd like to try to slow down the transmission in hopes that whatever's causing the 'Service Not Available' condition will be placated if the submissions occur asynchronously. But from the looks of the code, it already is since i'm using a Try | catch block.
Here's the relevant bit of my facade code:
foreach (string email in recipientEmails)
{
try
{
EmailGateway.Instance.SendEmail(email, notificationSubject, notificationMessage);
}
catch (Exception ex)
{
Logger.Instance.LogException(ex);
Logger.Instance.LogMessage("ERROR! Unsent email is to: " + email );
}
Logger.Instance.LogMessage("Sent " + notificationSubject + " to " + email);
}
And here's the Gateway code (using System.Net.Mail;):
public virtual void SendEmail(string replyToAddress, string toEmailAddress, string subject, string body)
{
string destinationEmailAddress = toEmailAddress;
string fromEmailAddress = "[email protected]";
bool useSSL = "true";
MailMessage message = new MailMessage(fromEmailAddress, destinationEmailAddress, subject, body);
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = useSSL;
smtp.Send(message);
}
So i'm catching both successes and fails into my logger table. What I don't understand is how I can see a log message for both a fail and then a success condition for the same email address. That indicates a 'retry' and, while i'm not surprised that the SmtpClient (the native .net assembly) can retry without explicit code asking it to, I don't see how my facade's code is being made to log both conditions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SmtpClient 不会重试发送电子邮件。
在您的外观代码中,无论您是否收到异常,您总是会记录成功。
您应该在 try 块中记录成功,否则您将捕获异常(记录失败),从 catch 块中出来并记录成功,这就是您所观察到的。
SmtpClient is not retrying to send the email.
In your facade code as it is, you are always logging a success, whether you are getting an exception or not.
You should log success in the try block, otherwise you are catching the exception (logging failure), coming out of the catch block and logging success anyway, which is what you are observing.