Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.... I have developed an application where I want to send email. But it's getting me an error "An attempt was made to access a socket in a way forbidden by its access permissions [2607:f8b0:400e:c02::6d]:587". Please Help me. Thanks in advance.

Web.Config File:

XML
<configuration>

    <system.web>
      <compilation debug="false" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
  <appSettings>
    <add key="SendMail" value="true"/>
  </appSettings>
</configuration>


CS File:

C#
protected void Button1_Click(object sender, EventArgs e)
    {

        MailMessage mail = new MailMessage("xxxxxxxx@gmail.com", "xxxxxxxx@gmail.com");
        mail.Subject = "Mail Message";
        mail.Body = TextBox1.Text;
        SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
        sc.Credentials = new System.Net.NetworkCredential()
        {
            UserName = "xxxxxxxx@gmail.com",
            Password = "********"
        };
        sc.EnableSsl = true;
        sc.Send(mail);// My browser shows above error at this point.
        }
[Edit]Email address removed (changed original address into xxxxxxxx@gmail.com)[/Edit]
Posted
Updated 17-May-13 6:21am
v4
Comments
Sergey Alexandrovich Kryukov 17-May-13 12:19pm    
Make sure you can send e-mail throw the same service with these credentials. For check up, you can use some available mail program.
—SA
bbirajdar 17-May-13 12:19pm    
If you are sending email over SSl , then use SMTP Port 465
Chiklu.Soumya 17-May-13 12:25pm    
Yes I had used 465 port no. But it's getting same error. Please find error in my code. Thanks for your reply.
Thomas Daniels 17-May-13 12:26pm    
Never post your email address, if you don't like spam! If you want to know why you shouldn't post your email address, have a look here:
http://ask-leo.com/why_shouldnt_i_post_my_email_address_in_a_public_forum.html[^]
Chiklu.Soumya 17-May-13 12:30pm    
Thankx for updating my quest. Will not commit this mistake again.

1 solution

Hello @Chiklu.Soumya,

Please follow my answer - sending email to gmail from asp.net[^].

This code is working perfectly.

Please replace your email ids and passwords correctly and test.

In case you face any difficulty, please reply me.
 
Share this answer
 
Comments
Chiklu.Soumya 18-May-13 4:24am    
Thannks For your reply. But your code getting this error. Please find a solution. Once again Thank You.

"System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions [2607:f8b0:400e:c02::6c]:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.Page_Load(Object sender, EventArgs e)System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions [2607:f8b0:400e:c02::6c]:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)System.Net.Sockets.SocketException (0x80004005): An attempt was made to access a socket in a way forbidden by its access permissions [2607:f8b0:400e:c02::6c]:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) "
Ok. The problem may be due to the Firewall or Antivirus preventing port 587 to send the mail.
So, try making your Firewall and Antivirus off. Then send a mail with the code.
Chiklu.Soumya 18-May-13 9:09am    
Thank you so much............. Tadit Dash.I can't express my happiness with your code. Thank you once again. Your code works perfectly. I"ll never forget your help. I added an exception to windows firewall for port no 587. Then i turned off antivirus. Finally it worked.
Most welcome buddy. My pleasure... :)
I am glad that it helped. And this appreciation means a lot to me, my friend.

Thanks a lot for appreciating and accepting the answer....

Regards,
Tadit
Member 12959647 20-Jan-17 6:39am    
Does gmail has made any additional securities recently. I tried all ways but couldn't able to send mail after hosting. But can send in localhost. Am getting same error as above

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900