"在使用SMTP时,尝试以禁止其访问权限的方式访问套接字"

SPa*_*dya 20 c# sockets asp.net email smtp

我正在尝试在数据库中的某些值超过其阈值时发送邮件c#.

我已经在Windows防火墙中允许端口25,587和465.并禁用防病毒软件中防止群发邮件的选项.我正在使用的代码如下

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);
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪

[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:\Users\spandya\Documents\Visual Studio 2012\WebSites\WebSite3\Admin\Alert.aspx.cs:61
   Admin_Alert.Page_Load(Object sender, EventArgs e) in c:\Users\spandya\Documents\Visual Studio 2012\WebSites\WebSite3\Admin\Alert.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
Run Code Online (Sandbox Code Playgroud)

我还缺少什么呢?防火墙入站规则适用于这些特定的端口地址.

Gle*_*rie 19

请确认您的防火墙允许出站流量,并且您没有被防病毒软件阻止.

我收到了同样的问题,罪魁祸首是杀毒软件.


Sla*_*des 13

好的,非常重要的是要意识到这里的含义.

文档说SmtpClient不支持超过465的SSL.

似乎您别无选择,只能使用您的邮件主机可能不支持的STARTTLS.如果主机需要使用超过465的SSL,则可能必须使用其他库.

引用自http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx

SmtpClient类仅支持RFC 3207中定义的基于传输层安全性的SMTP服务扩展.在此模式下,SMTP会话从未加密的通道开始,然后客户端向服务器发出STARTTLS命令以切换到使用SSL进行安全通信.有关详细信息,请参阅Internet工程任务组(IETF)发布的RFC 3207.

备用连接方法是在发送任何协议命令之前预先建立SSL会话.此连接方法有时称为SMTP/SSL,SMTP over SSL或SMTPS,默认情况下使用端口465.目前不支持使用SSL的备用连接方法.


Tom*_*zek 8

我收到此错误:

System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions
Run Code Online (Sandbox Code Playgroud)

端口被另一个程序使用时