Click here to Skip to main content
15,885,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a page to send email through my company's SMTP email server. It worked well before. But I got Exception: "Unable to connect to remote server, An attempt was made to access a socket in a way forbidden by its access permissions" even though I did not change anything on the code. How can this problem get fixed? Thanks in advance.
Posted

1 solution

The issue appears to be a change made to your company's SMTP server.

Without seeing your code, it's difficult to guess.
Maybe the SMTP server previously allowed unauthenticated connections and now it doesn't?
 
Share this answer
 
Comments
[no name] 15-Apr-14 9:08am    
Checked w/ SMTP Server admin and was told no change on his side. My code is below. Thanks.
SmtpClient Smtp_Server = new SmtpClient();
MailMessage e_mail = new MailMessage();
Smtp_Server.UseDefaultCredentials = false; // true or false - no effect
Smtp_Server.Credentials = new System.Net.NetworkCredential();
Smtp_Server.Port = 25;
//Smtp_Server.EnableSsl = true;
Smtp_Server.Host = "mailhost.myCompany.com";
Smtp_Server.UseDefaultCredentials = false;
e_mail.From = new MailAddress(txtFrom.Text);
e_mail.To.Add(txtTo.Text);
e_mail.Subject = txtSubject.Text;
e_mail.Body = txtMessage.Text;
e_mail.IsBodyHtml = false;
Smtp_Server.Send(e_mail); // Error: Send email failed
hypermellow 15-Apr-14 9:30am    
Your code shows you are trying to send an unauthenticated email via SMTP.

If I try the same, using an SMTP server that does not allow unauthenticated relaying of messages I received the following exception:

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 xxx.xxx.xxx.xxx:25\r\n at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)\r\n at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)\r\n --- End of inner exception stack trace ---\r\n at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)\r\n at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)\r\n at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)\r\n at System.Net.
ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)\r\n at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)\r\n at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)\r\n at System.Net.Mail.SmtpClient.GetConnection()\r\n at System.Net.Mail.SmtpClient.Send(MailMessage message)\r\n --- End of inner exception stack trace ---\r\n at System.Net.Mail.SmtpClient.Send(MailMessage message)\r\n at xxxx.xxxx.xxxx.myMethod(String sFirstName, String sSurname, String sEmailAddress, String sPendingAssessment) in \\\\<server>\\<path to website>\\App_Code\\<my class="">.cs:line xxx

You can compare this exception to you own.

What happens when you remove the line:
Smtp_Server.Credentials = new System.Net.NetworkCredential();

If your code previously worked, and your running it from the same location, it looks like there has been a change to your SMTP server config.
[no name] 16-Apr-14 7:13am    
To: hypermellow: I agree your point: my SMTP server config may have some changes. I will check with our SMTP server's admin. Thanks.

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