Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

c# - "An attempt was made to access a socket in a way forbidden by its access permissions" while using SMTP

I am trying to send an SMTP email when certain values in database crosses its threshold value.

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("[email protected]");
        // From
        MailAddress mailAddress = new MailAddress("[email protected]");
        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);

Stack Trace

[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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please confirm that your firewall is allowing outbound traffic and that you are not being blocked by antivirus software.

I received the same issue and the culprit was antivirus software.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...