问题描述
|
我正在使用一个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 = \"my@address.com\";
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程序集)可以在没有显式代码要求的情况下重试并不感到惊讶,但我看不到外观的代码如何记录两个条件。
解决方法
SmtpClient不会重试发送电子邮件。
在外观代码中,无论是否获取异常,您总是在记录成功。
您应该在try块中记录成功,否则将捕获异常(记录失败),从catch块中出来并无论如何记录成功,这就是您要观察的内容。
foreach (string email in recipientEmails)
{
try
{
EmailGateway.Instance.SendEmail(email,notificationSubject,notificationMessage);
Logger.Instance.LogMessage(\"Sent \" + notificationSubject + \" to \" + email);
}
catch (Exception ex)
{
Logger.Instance.LogException(ex);
Logger.Instance.LogMessage(\"ERROR! Unsent email is to: \" + email );
}
}