【问题标题】:System.Net.Mail.SmtpException : SMTP command timeout - closing connectionSystem.Net.Mail.SmtpException:SMTP 命令超时 - 关闭连接
【发布时间】:2012-05-23 07:50:30
【问题描述】:

我遇到了偶尔发生的错误。当我遇到此错误时,如果我再试一次,电子邮件将发送。我无法可靠地重现该错误,但它经常出现,足以成为一个问题。

System.Net.Mail.SmtpException : 服务不可用,正在关闭传输通道。服务器响应为:[服务器名称]:SMTP 命令超时 - 关闭连接

堆栈跟踪:

在 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport .SendMail(MailAddress 发件人,MailAddressCollection 收件人,String deliveryNotify,SmtpFailedRecipientException& 异常) at System.Net.Mail.SmtpClient.Send(MailMessage message)

这是有问题的代码。它使用 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="no-reply@mydomain.com">
          <network host="smtp.emailhost.com" userName="someaddress@mydomain.com" 
              password="myPassword1" port="2500" />
        </smtp>
      </mailSettings>
  </system.net>

【问题讨论】:

    标签: c# email structuremap smtpclient system.net.mail


    【解决方案1】:

    您是否在发送后处理 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);
    }
    

    【讨论】:

    • 我们面临同样的问题,我们使用 AWS SMTP,但使用此解决方案后仍然存在同样的问题。不知道该怎么办。有没有其他方法可以摆脱...?
    • @sbmandav 我在使用 AWS SMTP 时也遇到了这个问题,我还可以验证这个解决方案没有帮助。
    • 我在使用 AWS SMTP 时也遇到了同样的问题。有没有人看到专门针对 AWS 的解决方案?
    猜你喜欢
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 2022-01-20
    • 2014-01-11
    相关资源
    最近更新 更多