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,
using (SmtpClient client = new SmtpClient("smtp.example.com", 25)) {
client.EnableSsl = true;
client.Credentials = new NetworkCredentials("username", "password");
MailMessage message = new MailMessage {
From = "",
To = "",
Subject = "",
Body = ""
};
}
But as soon as code hits this function,
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.