0

I want to test connection is established without sending mail.My code is below.I have put wrong email and password but i m not getting any exception.I don't want to test with email send.

try {
    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
    NetworkCredential credentials = new NetworkCredential(textboxprimarymail.Text, textBoxpassprimary.Text);
    smtpClient.Credentials = credentials;
    connectionestablish.Text = "Connetion Established";
    connectionestablish.ForeColor = System.Drawing.Color.ForestGreen; 
}
catch  {
    connectionestablish.Text = "Connetion Error";
    connectionestablish.ForeColor = System.Drawing.Color.Red; 
}
Lorenzo
  • 29,081
  • 49
  • 125
  • 222
Ajay_Kumar
  • 1,381
  • 10
  • 34
  • 62
  • possible duplicate of [How to validate smtp credentials before sending mail in C# ?](http://stackoverflow.com/questions/2426098/how-to-validate-smtp-credentials-before-sending-mail-in-c) – Randy Levy Dec 05 '10 at 00:55

1 Answers1

0

This question is a duplicate. Basically, there is no way. As stated in another answer here:

There is no way.

SmtpClient can not validate the usernamepassword without contacting the serve it connects to.

What you could do is do it outside SmtpClient, by opening a TCP connection to the server... but authentication CAN be complicated depending how the server is configured.

May I ask WHY you need to know that before sending? normal behavior IMHO would be to wrap sending in appropriate error handling to catch exceptions here.

How to validate smtp credentials before sending mail?

Community
  • 1
  • 1
Robin Orheden
  • 2,714
  • 23
  • 24