2011-04-14 72 views
0

我正在针对GoogleApps帐户从我的(mvc)网络应用发送事件通知。通常一切正常。当系统要求发送SMTP服务器超过75个消息或使我看到回复:了解SmtpClient如何处理重试

服务不可用,关闭 传输通道。服务器 响应是:4.7.0稍后再试, 关闭连接。 (MAIL) uf10sm1153163icb.17

然而,该系统是自动重试和任何我的系统被要求送最后(通过一切我可以告诉,因为这点),使其出来。但考虑到代码如何生成和发送电子邮件,我不知道如何处理这些重试。

我想尝试减慢传输速度,希望如果提交异步发生,任何导致“服务不可用”状况的问题都将被安排。但从代码的外观来看,它已经是我使用Try | catch块。

这里是我的门面代码中的相关位:

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程序集)可以重试没有明确的代码要求,但我不明白我的外观的代码是如何记录这两个条件。

回答

3

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); 
    } 
}