There are dozen of questions asking is smtp server working like Testing SMTP server is running via C# and Can I test SmtpClient before calling client.Send()? and they do they work of checking by sending commands to smtp servers. They never use any credentials for testing purposes. So I'm wondering is there a way to check are these login and password valid on this smtp-server without sending a mail?
Asked
Active
Viewed 5,165 times
2 Answers
3
My big concern when I was working on this issue is to find a solution where I do not have to send email to check if the credentials are valid or not. So after trying a bunch of the solutions offered online, I came up with my own acceptable one to test email credentials :
I use MailKit to help me out.
public static bool ValidateCredentials(string username, string password, string server, int port, bool certificationValidation)
{
try
{
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
try
{
client.ServerCertificateValidationCallback = (s, c, h, e) => certificationValidation;
client.Connect(server, port, false);
client.Authenticate(username, password);
client.Disconnect(true);
return true;
}
catch (Exception ex)
{
Console.WriteLine(string.Format("ValidateCredentials Exception: {0}", ex.Message));
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(string.Format("ValidateCredentials Exception: {0}", ex.Message));
}
return false;
}

Samidjo
- 2,315
- 30
- 37
1
SMTP doesn't allow you login without sending an email.
have a look at the below:

Community
- 1
- 1

Massimiliano Peluso
- 26,379
- 6
- 61
- 70