Sunday, 24 August 2014

Error connecting to SMTP Server

Problem:

All of you would have faced issues when connecting to a remote server on ceertain ports. The issue will be strange to trouble shoot as the same connection works from a different network or on the server and so on. I came across such a situation when building and testing a mailing component and I experienced that the .NET SmtpClient component was encountering exception. It took considerable time for me to trouble shoot and get it resolved.

Solution:

My first take was to try whether the credentials that I have used is just fine, by creating an email account in outlook and that worked fine. I just wanted to isolate various probable areas of concern. As such I then tried using an online SMTP server test service, which was showing an error that the client must first check for incoming mails. Telnet to the mailserver on port 25 also gave an error. I then examined our mail server, whether it has any such restrictions, but I could not find any specific restriction that could cause this issue.

My next step was to examine the details of the exception for any possible clues. The inner exception nested couple of levels deep revealed the error as "An attempt was made to access a socket in a way forbidden by its access permissions"

I then tried looking for solutions online there were many posts and one solution was about to try the following and one of that could solve this issue:
  • Restart the windows firewall service
  • Reboot the problematic machine
  • Restart the “TCP/IP stack”. Run CMD as administrator, type “netsh int ip reset resetlog.txt” to reset TCP/IP.
  • Try to temporarily disable antivirus.
After a bit of analysis, I could find that in my case it was the McAfee Endpoint Protection. I could find that the rule "Prevent mass mailing worms from sending mail" was set to block connections on port 25 with certain exceptions. I added the VWDExpress.exe to the exception list and then it started working fine.