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

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

how to test connection is established in smtp in c# windows

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; 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...