2011-04-14 70 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); 
    } 
}