How can I sort of 'ping' an SMTP server so I can check if the account/password is a valid one?
I'm trying to build a little WPF email sender tutorial but I don't want to share it if it has this glaring bug.
Currently, this is what happens.
- user types in username and password.
- label 'loginstatus' changes to "Logged in" regardless if it's gibeerish or not.
- Message body and send to fields are enabled.
- user开发者_如何学JAVA pressed "Send" button and if there is an exception (for example, wrong username/password) the messagebox shows it, and loginstatus is changed back to logged out.
This is very very wrong and I want to fix it.
How can I just 'ping' to see if a credential is correct (without sending a test email).
I'm using smtp.gmail.com port 587
Edit Here's how I'm sending the emails.
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential(username, password),
EnableSsl = true
};
try
{
client.Send(fromEmail, toEmail, subject, body);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
You can't do this with System.Net.Mail. You have to either roll your own or used a 3rd party product (shameless plug: like mine-- www.aspnetmx.com )
If you have control over the SMTP communication you can only do the login part to test username and password without sending a message.
精彩评论