Hello
I've prepared a code which sends mail. I've a problem in trying to catch the exceptions that the code throws.
1. I need to check if the internet connection is open
2. I need to check if the password and E-mail Id entered by the user are correct

In both these cases the exception thrown by the code is:

System.Net.Mail.SmtpException

In such a case, I cannot ascertain the reason for which the exception is being thrown just from the exception string. How do I go about solving this problem? Please help.
Thanks in advance :)

>I need to check if the internet connection is open

Use Ping from System.Net.NetWorkInformation namespace.

try
            {
                Ping ping = new Ping();
                string host = "daniweb.com";
                int timeout = 4000;
                PingReply reply = ping.Send(host, timeout);
                if (reply.Status == IPStatus.Success)
                {
                    MessageBox.Show("Ok!");
                }
                else
                {
                    MessageBox.Show("Sorry!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sorry!");
            }

> I need to check if the password and E-mail Id entered by the user are correct

Without sending email you can't validate username and password.

I've actually used this code, I was looking for a solution in cases as the above in which 2 different problems throw the same exception, is there any way by which we can solve the above problem by using try and catch?
Thank you for your reply. :)

Did you check the Message property of the exception to see what it says?

I did not get your question.
If u mean to say whether I checked the exception in a message box, then yes, I got the exception I mentioned above.
The exception is same for both cases,however there are several parsed exceptions which are different. Is there a way I can use them?
I hope I was clear about what I wanted to say.
Thank you for the reply :)

No, you didn't get my question. Every exception has a Message property. When you catch the exception, display the Message. For example:

catch (Exception e) {
    Console.WriteLine(e.Message);
}

yes I did this...this gave me the exception

System.Net.Mail.SmtpException

when I tried to work my code with a wrong password.
Again I got the same exception when I tried out my code with no internet connection.
This is what I'm asking, what do I do when two such checks give the same exception, I cannot write:

catch(System.Net.Mail.SmtpException)
{
messagebox.show("No internet connection")
}
catch(System.Net.Mail.SmtpException)
{
messagebox.show("Incorrect password")
}

In that case compiler does not know which message to show...it always shows the first one.Is there a way to solve this type of a problem?

So I wrote a quick program to test this. With an invalid host I got the Message (from the exception) "Failure Sending Mail". The InnerException says "The remote name could not be resolved". I get the same error with no internet connection.

When sending with invalid credentials I get "Mailbox unavailable: The server response was must be authenticated" (from SmtpFailedRecipientException);

So you aren't looking at the Message property if all you are getting is SmtpException.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.