web.config中:@H_502_3@
<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>
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 size) at System.Net.PooledStream.Write(Byte[] buffer,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 index1)
我试图在亚马逊论坛上获得帮助,但没有任何运气.似乎有些东西干扰了连接,但我不知道什么.有任何想法吗?谢谢.@H_502_3@
解决方法
When I do not dispose of the SmtpClient,I get the error…@H_502_3@
‘Service not available,closing transmission channel. The server
response was: Timeout waiting for data from client.’@H_502_3@
这实际上是间歇性问题的一个很好的解释,即根据执行的代码路径,这个问题可能会触发到Amazon SES挂起的SMTP连接,这显然是通过切换到另一个端口来解决,这意味着连接被重置 – 这就是SmtpClient.Dispose Method确保:@H_502_3@
Sends a QUIT message to the SMTP server,gracefully ends the TCP
connection,and releases all resources used by the current instance of
the 07003 class.@H_502_3@
因此,如Getting Started with Amazon SES and .NET所示,适当的模式将是促进using Statement:@H_502_3@
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,我强烈建议避免由于通常隐含的发送限制,请参阅下面的附录,了解各个Amazon EC2限制的详细信息.@H_502_3@
祝你好运!@H_502_3@
附录@H_502_3@
Amazon EC2限制端口25@H_502_3@
您可能会意识到这一点(我实际上不认为这是一个问题),但Amazon EC2通过端口25发送的电子邮件设置默认发送限制,并且如果您尝试超过使用Amazon SES时仍然适用的限制,见Amazon SES SMTP Issues:@H_502_3@
You are sending to Amazon SES from an Amazon EC2 instance via port 25
and you cannot reach your Amazon SES sending limits or you are
receiving time outs — Amazon SES EC2 imposes default sending limits on
email sent via port 25 and throttles outbound connections if you
attempt to exceed those limits. To remove these limits,submit a
07009. You can also connect to
Amazon SES via port 465 or port 587,neither of which is throttled.@H_502_3@
因此,我将删除端口25的测试/切换场景,并使用端口465/587,以避免虚假的引导(引用你也可以要求得到这个限制,但是需要几个小时,端口25似乎最好避免在第一位) – 有点不幸的是,几个官方的亚马逊SES样品正在使用端口25,甚至没有提到这个容易触发的问题确实.@H_502_3@