How to check smtp credentials before sending mail?

I need to check the username and password set in the SmtpClient instance before sending mail. Using this code:

 SmtpClient client = new SmtpClient(host); client.Credentials = new NetworkCredential(username,password); client.UseDefaultCredentials = false; // Here I need to verify the credentials(ie username and password) client.Send(mail); 

How to check if the user is allowed to identify credentials for connecting and sending mail?

+6
smtpclient
source share
6 answers

There is no way.

SmtpClient in basic mode cannot verify the username password without contacting the service to which it connects.

What you can do is do it outside of SmtpClient by opening a TCP connection to the server ... but authentication can be complicated depending on the server configuration.

May I ask WHY you should know what before sending? IMHO's normal behavior would be to wrap the sending in appropriate error handling to catch exceptions here.

+3
source share

My production code to verify username and password before sending mail:

 public static bool ValidateCredentials(string login, string password, string server, int port, bool enableSsl) { SmtpConnectorBase connector; if (enableSsl) { connector = new SmtpConnectorWithSsl(server, port); } else { connector = new SmtpConnectorWithoutSsl(server, port); } if (!connector.CheckResponse(220)) { return false; } connector.SendData($"HELO {Dns.GetHostName()}{SmtpConnectorBase.EOF}"); if (!connector.CheckResponse(250)) { return false; } connector.SendData($"AUTH LOGIN{SmtpConnectorBase.EOF}"); if (!connector.CheckResponse(334)) { return false; } connector.SendData(Convert.ToBase64String(Encoding.UTF8.GetBytes($"{login}")) + SmtpConnectorBase.EOF); if (!connector.CheckResponse(334)) { return false; } connector.SendData(Convert.ToBase64String(Encoding.UTF8.GetBytes($"{password}")) + SmtpConnectorBase.EOF); if (!connector.CheckResponse(235)) { return false; } return true; } 

More details in the answer to a similar question .

Github code

+2
source share

Confirmation before sending mail is not possible with the SMTP client class in .net 2.0.

Trying to check manually by opening the port is as good as writing your own SMTP client, it is complicated.

+1
source share

.Net SmtpClient does not allow you to log in without sending a message.

You can verify your credentials by sending a test message to some existing address that ignores incoming email and checks to see if you get an exception.

Please note that these test emails will appear in your folder sent by your account, if any (for example, Gmail)

+1
source share

Try the following:

 try { smtpClient.Send(new MailMessage(" test@test.com ", " test@test.com ", "test", "test")); return string.Empty; } catch (SmtpFailedRecipientException) { return string.Empty; } catch (Exception ex) { return string.Format("SMTP server connection test failed: {0}", ex.InnerException != null ? ex.InnerException.Message : ex.Message); } 

It works for me in my validation method. But if you only need to verify credentials when sending emails, then just combine the sending with try ... catch.

+1
source share

I agree with the previous answers that SmtpClient cannot check without submitting.

But maybe your problem can be solved in any case: if the username or password is incorrect, client.Send(mail); will throw an exception. You can create a while and try-catch-block loop around the send, catch the exception and ask the user for the correct username and password, and then try again. If the user clicks the Cancel button in the dialog box or sends deleted data without exception, you exit the while loop.

0
source share

All Articles