System.Net.Mail.SmtpException : SMTP 命令超时 - 关闭连接

本文关键字:超时 连接 命令 SMTP Net Mail SmtpException System | 更新日期: 2023-09-27 18:34:11

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

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

堆栈跟踪:

at 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 sender, MailAddressCollection 收件人, String deliveryNotify, SmtpFailedRecipientException& exception( 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>

System.Net.Mail.SmtpException : SMTP 命令超时 - 关闭连接

发送后是否释放了 Smtp 客户端对象? 从 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);
}