“尝试以访问权限禁止的方式访问套接字"使用 SMTP 时
当数据库中的某些值超过其阈值时,我尝试发送 SMTP 电子邮件.
I am trying to send an SMTP email when certain values in database crosses its threshold value.
我已经在 Windows 防火墙中允许端口 25,587 和 465,并禁用了防病毒软件中阻止群发邮件的选项.我正在使用的代码如下
I have already allowed ports 25,587 and 465 in the Windows firewall and disabled the option of preventing mass mail in the Antivirus. The code I am using is given below
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add("to@domain.com");
// From
MailAddress mailAddress = new MailAddress("from@domain.com");
mailMsg.From = mailAddress;
// Subject and Body
mailMsg.Subject = "MCAS Alert";
mailMsg.Body = "Parameter out of range";
SmtpClient smtpClient = new SmtpClient("smtp.servername.com", 25);
smtpClient.UseDefaultCredentials = false;
smtpClient.Timeout = 30000;
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("username", "passwrod");
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = true;
//ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtpClient.Send(mailMsg);
堆栈跟踪
[SocketException (0x271d): An attempt was made to access a socket in a way forbidden by its access permissions xx.xx.xx.xx:25]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +208
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +464
[WebException: Unable to connect to the remote server]
System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) +6486360
System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) +307
System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +19
System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +324
System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) +141
System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) +170
System.Net.Mail.SmtpClient.GetConnection() +44
System.Net.Mail.SmtpClient.Send(MailMessage message) +1554
[SmtpException: Failure sending mail.]
System.Net.Mail.SmtpClient.Send(MailMessage message) +1906
Admin_Alert.SMTPAuth() in c:UsersspandyaDocumentsVisual Studio 2012WebSitesWebSite3AdminAlert.aspx.cs:61
Admin_Alert.Page_Load(Object sender, EventArgs e) in c:UsersspandyaDocumentsVisual Studio 2012WebSitesWebSite3AdminAlert.aspx.cs:22
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
我还缺少什么?防火墙入站规则适用于这些特定端口地址.
What else I am missing here? Firewall inbound rules are there for these specific port addresses.
好的,意识到这里的含义非常重要.
Ok, so very important to realize the implications here.
文档说 SmtpClient 不支持 465 以上的 SSL.
Docs say that SSL over 465 is NOT supported in SmtpClient.
似乎您别无选择,只能使用您的邮件主机可能不支持的 STARTTLS.如果您的主机要求使用 SSL over 465,您可能需要使用不同的库.
Seems like you have no choice but to use STARTTLS which may not be supported by your mail host. You may have to use a different library if your host requires use of SSL over 465.
引自 http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx
SmtpClient 类仅支持 RFC 3207 中定义的基于传输层安全的安全 SMTP 的 SMTP 服务扩展.在此模式下,SMTP 会话在未加密的通道上开始,然后客户端向服务器发出 STARTTLS 命令切换到使用 SSL 的安全通信.有关详细信息,请参阅互联网工程任务组 (IETF) 发布的 RFC 3207.
The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.
另一种连接方法是在发送任何协议命令之前预先建立 SSL 会话.这种连接方法有时称为 SMTP/SSL、SMTP over SSL 或 SMTPS,默认情况下使用端口 465.目前不支持这种使用 SSL 的替代连接方法.
An alternate connection method is where an SSL session is established up front before any protocol commands are sent. This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.