Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to validate the username and password set in an 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(i.e. username and password)
client.Send(mail);

How can I validate whether the user identified by the credentials is allowed to connect and send a mail?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

There is no way.

SmtpClient bascially 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...