简体   繁体   中英

How can I verify if the username and password matches before sending email using SmtpClient?

I have a very simple email form that sends out the email using department email account to given recipients. However, the company makes the password expire every 90 days, and there's a web form that I created where the authorized user can go and change the password/email address for the form.

Here's the code:

 SmtpClient smtp = new SmtpClient();
 smtp.Host = host;
 smtp.UseDefaultCredentials = false;
 smtp.Credentials = new System.Net.NetworkCredential(username, password);

 try
 {
      smtp.Send(iEditMail);
 }
 catch(SmtpException exception)
 {
     return Request.CreateResponse(HttpStatusCode.InternalServerError, exception.Message + " Make sure the username and password is correct.");
 }

Since user is responsible for entering the password, they may accidentally enter the wrong password for the email account or the password may expire. I need to be able to catch this so it can be notified to users who are trying to send email. (Btw, this is only for departmental use). How can I make sure if the credentials are correct before sending the email. Right now it doesn't throw any error to tell the email wasn't sent - we just don't receive it.

First:

If you can, it is better to create a service account and use those credentials. That way there is no need to change the credentials periodically. (@mason's suggestion)

However, if that is not possible:

According to MSDN, the SmtpClient.Send function throws SmtpException if authentication failed. That stinks because that same exception can mean various other things too. But it sounds like you want to verify the user name and password without sending an email. The SMTP protocol certainly allows that, but the SMTPClient class only provides methods for sending email. I don't know of any built-in .NET class that provides what you want.
So I think you need to either

  1. Write your own SMTP code using TCP.
  2. Find a third-party SMTP client that provides more functionality..
  3. Send a dummy email to a dummy address, and look at the StatusCode property to see if there was an authentication problem.

Option number 1 is hard if you care about SSL. If you just use plain text, I think authenticating to SMTP isn't that hard. But more and more mail servers are forbidding that. Option 3 is a hack, but should be easy.

EDIT: Added details about the StatusCode property.

The SMTPClient's Send(MailMessage) method raises a SmtpFailedRecipientsException exception if one or more recipient addresses were incorrect or unreachable.

Sample Code :

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);
}

When authenticating, store the username and password in the session. Compare the values of the session with user entries . If bool==true then send the email or else dont.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM