开发者

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.

  1. user types in username and password.
  2. label 'loginstatus' changes to "Logged in" regardless if it's gibeerish or not.
  3. Message body and send to fields are enabled.
  4. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜