2012-05-15 190 views
1

我有一個零星發生的錯誤。當我遇到這個錯誤時,如果我再試一次,電子郵件會發送。我無法可靠地重現錯誤,但經常發生問題。System.Net.Mail.SmtpException:SMTP命令超時 - 關閉連接

System.Net.Mail.SmtpException:服務不可用,關閉傳輸通道。服務器響應爲:[服務器]:SMTP命令超時 - 關閉連接

堆棧跟蹤:

在System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode的StatusCode,字符串響應)在在System.Net System.Net.Mail.SmtpTransport.SendMail(MailAddress發件人,MailAddressCollection收件人,字符串deliveryNotify,SmtpFailedRecipientException異常)System.Net.Mail.MailCommand.Send(SmtpConnection conn,字節[]命令,字符串從)。 Mail.SmtpClient.Send(MailMessage消息)

下面是有問題的代碼。它使用一個HTML模板(數據庫由一個web url默認備份驅動)來注入值並創建一個html電子郵件。

public void AccountActivationEmail(Common.Request.Email.AccountActivation request) 
{ 
    var profile = profileRepository.GetOne(new ProfileByUsername(request.Username, request.SiteId)); 
    var options = siteService.GetOptions(new GatewayOptionsRequest() 
    { 
     SiteId = request.SiteId 
    }); 
    var site = options.Site; 

    ExpiringHMAC hmac = new ExpiringHMAC(request.ExpireOn, new string[] { request.AccountId.ToString(), request.AccountKey.ToString(), request.Username }); 

    Dictionary<string, string> data = new Dictionary<string, string>(); 

    data.Add("{{logourl}}", String.IsNullOrEmpty(options.FullyHostedGateway.LogoUrl) ? UrlHelper.ConvertRelativeToAbsolute("/content/images/spacer.png") : options.FullyHostedGateway.LogoUrl); 
    data.Add("{{name}}", profile.Name.DisplayName); 
    data.Add("{{sitename}}", site.Name.DisplayName); 
    data.Add("{{activationlink}}", String.Format(ActivationUrlFormat, options.UrlFriendlyName, Encryption.EncryptQueryString(request.Username), hmac.ToString())); 

    MailDefinition template = new MailDefinition(); 
    MailMessage message = null; 
    var emailTemplate = options.GetEmailTemplate(EmailTemplateType.ActivateAccount); 
    string defaultSubject = "Activate Account"; 

    bool hasTemplate = false; 

    if (emailTemplate != null) 
    { 
     hasTemplate = !String.IsNullOrEmpty(emailTemplate.Template); 
    } 

    if (!hasTemplate) 
    { 
     template.BodyFileName = activationDefaultTemplateUrl; 
     message = template.CreateMailMessage(request.EmailAddress, data, new System.Web.UI.LiteralControl()); 
     message.IsBodyHtml = true; 
     message.Subject = defaultSubject; 
    } 
    else 
    { 
     message = template.CreateMailMessage(request.EmailAddress, data, emailTemplate.Template, new System.Web.UI.LiteralControl()); 
     message.IsBodyHtml = emailTemplate.IsHtml; 

     if (!String.IsNullOrEmpty(emailTemplate.Subject)) 
      message.Subject = emailTemplate.Subject; 
     else 
      message.Subject = defaultSubject; 
    } 

    if (options.ContactDetails != null) 
    { 
     if (!String.IsNullOrEmpty(options.ContactDetails.EmailAddress)) 
      message.From = new MailAddress(options.ContactDetails.EmailAddress); 
    } 

    SmtpClient client = new SmtpClient(); 

    client.Send(message); 
} 

此代碼是使用Structuremap生成爲單例的類的一部分。我想也許這可能會導致這個問題,但每次調用該方法時,都會創建一個新的SmtpClient對象,這將消除我看到的有關使用同一個連接發送多個電子郵件的問題。

我們沒有阻止或限制連接,這是我們的電子郵件託管就此問題採取的官方立場。我想看看是否有任何方法可以做得更好,所以我不會收到這些錯誤。

編輯:

我有我的郵件服務器在我的web.config中定義。我已通過電子郵件託管確認我的設置是正確的。

<system.net> 
     <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
      <network host="smtp.emailhost.com" userName="[email protected]" 
       password="myPassword1" port="2500" /> 
     </smtp> 
     </mailSettings> 
    </system.net> 

回答

2

您是否在發送後丟棄了Smtp Client對象?從http://connect.microsoft.com/VisualStudio/feedback/details/337557/smtpclient-does-not-close-session-after-sending-message看來,「......即使你每次創建一個SmtpClient的新實例,它仍然使用相同的會話。」

因此,也許

using (SmtpClient client = new SmtpClient()) 
{ 
    client.Send(message); 
} 
+0

我們都面臨着同樣的問題,我們使用AWS SMTP,但仍是同樣的問題,使用此解決方案後。不知道這是什麼待辦事項。有沒有其他方法可以擺脫...? – sbmandav

+0

@sbmandav我也遇到了AWS SMTP的這個問題,我也可以驗證這個解決方案沒有幫助。 – Mike

+0

我也遇到了與AWS SMTP相同的問題。有沒有人看到過專門針對AWS的解決方案? – agarcian