Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I want to validate SMTP server credentials before sending an email.

Tests that should be covered:
1. Incorrect port number
2. Incorrect SMTP Server
3. Incorrect User Name
4. Incorrect Password.

Note: I don't want to send any email during validation

Thanks in advance
Posted
Updated 13-Aug-18 23:03pm
Comments
[no name] 1-Aug-15 7:41am    
You don't.

First of all, I don't see why you would test validate the SMTP server without sending the email. Typically connection to SMTP server is configured once and then it's working. Base on that it would be a waste of network bandwith to always validate the existence of the server.

So I believe the best option is to try to send the mail and if unsuccessful then examine the error you get. This is nicely explained in Sending emails over .NET framework, and general problems – using C# code[^]

If you really need to try to validate (partially) the SMTP server, I think the 'best' way would be to communicate with the server with sockets and try the operations step-by-step. In order to understand the communication, have a look at Understanding the Insides of the SMTP Mail Protocol: Part 1[^]
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 1-Aug-15 8:16am    
5ed; good solution. Also read my solution Solution 2 for a little more explanation of why nothing would work for him as a solution and he may need to work around with his own TCP client to communicate with that SMTP server.
First of all read Solution 1 and the article that Mika has referenced of mine, Sending emails over .NET framework, and general problems – using C# code[^] that article was written to cover a few basics about sending emails, and a few problems that might be generated while sending the email.

Now, what happens in SmtpClient object is that you do not get any error in the following code,
C#
using (SmtpClient client = new SmtpClient("smtp.example.com", 25)) { // Default TCP port
  client.EnableSsl = true;
  client.Credentials = new NetworkCredentials("username", "password");
  
  // Even the following lines are executed safely, 
  MailMessage message = new MailMessage {
                           From = "",
                           To = "",
                           Subject = "",
                           Body = ""
                        };
}


But as soon as code hits this function,

C#
client.Send(message);


In case of any error, the program breaks. Why? Because at this point, program communicates with the server and shares data. Server checks the authentication and responds. In case of any problem, program breaks. Problem can be any one of the following:

1. Hostname problems, server doesn't exist at that URI, check it again.
2. Wrong port number. You should know that this 25 is default TCP port, it won't cause problems unless server denies to respond.
3. Error in recipient or sender address. This also causes a problem if the email address is not correct. So, check it twice.
4. Network problems. :laugh: This should have come first. Stupid me.

So, you see there are many problems that may cause your program to break, and they are not easily testable until or unless you run that function to connect to the server itself and check for the details.

A very sample solution to this problem is to create your own Smtp client object using TCP client (TcpClient[^]) object. Then, initiate requests to the server to determine whether the server is ready to communicate (in case for hostname or port problems). Also try to run some functions to authenticate or not. Then send the emails on your own. :-) A long but a worthy solution in this case, that is the only way to go for you to solve the riddle.
 
Share this answer
 
Comments
Wendelius 1-Aug-15 8:38am    
Good explanation, 5. :)

However, I'd mention that doing the testing in phases, one-by-one it may reveal more information. Consider the following logic:
- ping the server, no reply => wrong address (or ping declined)
- ping successful, try open the SMTP port, fail => wrong port etc
- open successful try HELO and so on...
Afzaal Ahmad Zeeshan 1-Aug-15 11:00am    
Thank you, Mika.

Indeed or rather than simply pinging. He could create a custom TCP client to connect to that server. But that all relies on him. :)

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