Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 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 Reply

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...