亚马逊SES停止工作

时间:2013-01-28 13:33:56

标签: asp.net amazon-web-services smtp amazon-ses

我设置了亚马逊SES,它最初工作了几个小时,然后突然停了下来。我发送的所有电子邮件和我们的域名都已经过验证。我们不发送批量电子邮件 - 每天只发送几百封。每当我对web.config进行更改时,它似乎再次允许它再工作2-3个小时。例如,它停止工作,所以我将端口587切换到25,它开始工作2-3小时,然后停止。然后我换回587,同样的事情发生了。一旦它停止工作,它似乎永远不会自己重新开始。它运行在两个负载均衡的服务器上,asp.net framework v2.0,IIS 7.5。这是我正在使用的代码:

的web.config:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network" from="no-reply@ourdomain.com">
      <network defaultCredentials="false" host="email-smtp.us-east-1.amazonaws.com" userName="***" password="***" port="587" />
    </smtp>
  </mailSettings>
</system.net>

c#c​​ode:

var smtpClient = new SmtpClient() { EnableSsl = true };

var mailMessage =
    new MailMessage(fromAddress, toAddress, subject, body)
    {
        IsBodyHtml = true
    };

smtpClient.Send(mailMessage);

以下是我收到的两个错误:

The following exception was thrown by the web event provider '(null)' in the application '/' (in an application lifetime a maximum of one exception will be logged per provider instance):

System.Web.HttpException: Unable to send out an e-mail to the SMTP server. Please ensure that the server specified in the <smtpMail> section is valid. ---> System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.
   at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
   at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Mail.SmtpConnection.Flush()
   at System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn)
   at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at System.Web.Management.MailWebEventProvider.SendMail(MailMessage msg)
   --- End of inner exception stack trace ---
   at System.Web.Management.MailWebEventProvider.SendMail(MailMessage msg)
   at System.Web.Management.SimpleMailWebEventProvider.SendMessageInternal(WebBaseEventCollection events, Int32 notificationSequence, Int32 begin, DateTime lastFlush, Int32 discardedSinceLastFlush, Int32 eventsInBuffer, Int32 messageSequence, Int32 messagesInNotification, Int32 eventsInNotification, Int32 eventsLostDueToMessageLimit)
   at System.Web.Management.SimpleMailWebEventProvider.SendMessage(WebBaseEvent eventRaised)
   at *****.Global.SimpleMailWithSslWebEventProvider.ProcessEvent(WebBaseEvent raisedEvent)
   at System.Web.Management.WebBaseEvent.RaiseInternal(WebBaseEvent eventRaised, ArrayList firingRuleInfos, Int32 index0, Int32 index1)

The following exception was thrown by the web event provider '(null)' in the application '/' (in an application lifetime a maximum of one exception will be logged per provider instance):

System.Web.HttpException: Unable to send out an e-mail to the SMTP server. Please ensure that the server specified in the <smtpMail> section is valid. ---> System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: Timeout waiting for data from client.
   at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
   at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at System.Web.Management.MailWebEventProvider.SendMail(MailMessage msg)
   --- End of inner exception stack trace ---
   at System.Web.Management.MailWebEventProvider.SendMail(MailMessage msg)
   at System.Web.Management.SimpleMailWebEventProvider.SendMessageInternal(WebBaseEventCollection events, Int32 notificationSequence, Int32 begin, DateTime lastFlush, Int32 discardedSinceLastFlush, Int32 eventsInBuffer, Int32 messageSequence, Int32 messagesInNotification, Int32 eventsInNotification, Int32 eventsLostDueToMessageLimit)
   at System.Web.Management.SimpleMailWebEventProvider.SendMessage(WebBaseEvent eventRaised)
   at *****.Global.SimpleMailWithSslWebEventProvider.ProcessEvent(WebBaseEvent raisedEvent)
   at System.Web.Management.WebBaseEvent.RaiseInternal(WebBaseEvent eventRaised, ArrayList firingRuleInfos, Int32 index0, Int32 index1)

我试图在亚马逊论坛上获得帮助,但没有任何运气。似乎某些东西干扰了连接,但我不知道是什么。有任何想法吗?感谢。

3 个答案:

答案 0 :(得分:23)

这听起来确实是一个棘手的问题 - 我对此并非100%肯定,但您似乎错过了处理Sending email is often very slow after changing to VPC instance中提到的SMTP客户端导致您看到的错误:< / p>

  

当我不处理SmtpClient时,我收到错误......

     

'服务不可用,关闭传输通道。服务器   响应是:超时等待来自客户端的数据。'

对于间歇性问题,这实际上是一个很好的解释,即取决于执行的代码路径,此问题可能会触发到Amazon SES的SMTP连接挂起,这显然可以通过切换到另一个端口来解决,这意味着连接被重置 - 这也是SmtpClient.Dispose Method确保的:

  

向SMTP服务器发送QUIT消息,正常结束TCP   连接,并释放当前实例使用的所有资源   SmtpClient班。

相应地,适当的模式是促进using Statement,如Getting Started with Amazon SES and .NET所示:

  String username = "SMTP-USERNAME";  // Replace with your SMTP username.
  String password = "SMTP-PASSWORD";  // Replace with your SMTP password.
  String host = "email-smtp.us-east-1.amazonaws.com";
  int port = 25;

  using (var client = new System.Net.Mail.SmtpClient(host, port))
  {
    client.Credentials = new System.Net.NetworkCredential(username, password);
    client.EnableSsl = true;

    client.Send
    (
              "FROM@EXAMPLE.COM",  // Replace with the sender address.
              "TO@EXAMPLE.COM",    // Replace with the recipient address.
              "Testing Amazon SES through SMTP",              
              "This email was delivered through Amazon SES via the SMTP end point."
    );
  }

请注意,该示例使用的是端口25,由于通常隐含的发送限制,我强烈建议避免使用端口25,有关相应Amazon EC2限制的详细信息,请参阅下面的附录

祝你好运!


附录

Amazon EC2对端口25进行限制

您可能已经意识到这一点(我实际上并不认为这是一个问题),但是Amazon EC2 对通过端口25发送的电子邮件施加默认发送限制,并且如果您尝试尝试限制出站连接超出使用Amazon SES时仍然适用的限制,请参阅Amazon SES SMTP Issues

  

您正通过端口25从Amazon EC2实例发送到Amazon SES   并且您无法达到您的Amazon SES发送限制   接收超时 - Amazon SES EC2强加默认发送限制   如果您通过端口25发送的电子邮件和限制出站连接   试图超过这些限制。要删除这些限制,请提交   Request to Remove Email Sending Limitations。你也可以连接到   亚马逊SES通过端口465或端口587,两者都不受限制。

因此,我会删除测试/切换方案中的端口25并仅使用端口465/587以避免错误的线索(因为引用您也可以请求删除此限制但是它将需要几个小时和端口25似乎最好避免一开始) - 有点不幸的是,几个正式的亚马逊SES样本正在使用端口25,甚至没有提到这个容易触发的问题。

答案 1 :(得分:1)

虽然这个问题是针对.NET v2.0的,但我最近也在.NET v4.5上遇到过这个问题。即使添加使用SMTP客户端(如早期帖子中所示),我仍然间歇性地收到此错误。经过数小时的研究后,我得到了AWS对STARTTLS端口的支持(2587; http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-connect.html),并且,在非常精细的版本中,Microsoft声明当SMTP客户端上的“EnableSsl”为真时,客户端需要STARTTLS会话({{ 3}})。因此,在将我的端口更新为2587 在“使用{}”中围绕SmtpClient之后,我能够成功发送~20k的电子邮件,而无需重现错误。

因此,工作配置将是:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network">
      <network host="email-smtp.us-west-2.amazonaws.com" port="2587" enableSsl="true" password="password" />
    </smtp>
  </mailSettings>
</system.net>

一个免责声明是我仍然不完全了解AWS或.NET SMTP客户端的真实配置,因此这仍然有点间接。

答案 2 :(得分:0)

我也在使用AWS ses服务

 smtp deliveryMethod="Network" from="XXXXXXX@ZXXX.com"
    network enableSsl="true" host="email-smtp.us-east-1.amazonaws.com" userName="******" password="*********" port="25" 
 smtp

希望这适合你。