1

散発的にエラーが発生しています。このエラーが発生した場合、再試行すると電子メールが送信されます。エラーを確実に再現することはできませんが、問題になるほど頻繁に発生しています。

System.Net.Mail.SmtpException : サービスを利用できません。伝送チャネルを閉じています。サーバーの応答: [サーバー名]: SMTP コマンドのタイムアウト - 接続を閉じています

スタックトレース:

System.Net.Mail.MailCommand.CheckResponse (SmtpStatusCode statusCode、文字列応答) で System.Net.Mail.MailCommand.Send (SmtpConnection conn、Byte[] コマンド、文字列から) で System.Net.Mail.SmtpTransport.SendMail ( System.Net.Mail.SmtpClient.Send(MailMessage メッセージ) での MailAddress 送信者、MailAddressCollection 受信者、String deliveryNotify、SmtpFailedRecipientException& 例外)

これが問題のコードです。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>
4

1 に答える 1

2

Are you disposing of the Smtp Client object after sending? From http://connect.microsoft.com/VisualStudio/feedback/details/337557/smtpclient-does-not-close-session-after-sending-message it would appear that "...even if you are creating a new instance of the SmtpClient every time, it still uses the same unerlying session."

So perhaps

using (SmtpClient client = new SmtpClient())
{
    client.Send(message);
}
于 2012-05-15T19:04:24.903 に答える