【问题标题】:How to authenticate the password in an email application built on windows forms?如何在基于 Windows 窗体的电子邮件应用程序中验证密码?
【发布时间】:2013-03-07 05:00:57
【问题描述】:

昨天,我的上级分配了一项任务,要在 .net 中构建一个 Windows 窗体应用程序,该应用程序看起来就像我附上的图像。我做了有关电子邮件应用程序发送过程的所有工作,但我卡在一个地方,我无法弄清楚如何验证电子邮件表单中的密码。密码必须是在“发件人:”字段中提供的同一电子邮件。

这是我的表单背后的代码,

 public partial class Form1 : Form
{
    MailMessage message;
    SmtpClient smtp;
    public Form1()
    {

        InitializeComponent();
        lbl_Error.Visible = false;
    }

    private void chk_Show_Password_CheckedChanged(object sender, EventArgs e)
    {

        if (chk_Show_Password.Checked == true)
            txt_Password.PasswordChar= '\0';
        else
            txt_Password.PasswordChar='*';



    }

    private void btn_Send_Click(object sender, EventArgs e)
    {
        btn_Send.Enabled = false;
        txt_Password.Text = "";


        try
        {
            message = new MailMessage();
            if(isValidEmail(txt_From.Text))
            {
                message.From = new MailAddress(txt_From.Text);
            }

            if (isValidEmail(txt_To.Text))
            {
                message.To.Add(txt_To.Text);
            }

            message.Body = txt_Details.Text;

            //attributes for smtp
            smtp = new SmtpClient("smtp.gmail.com");
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("imad.majid90@gmail.com", "mypassword");
            smtp.Send(message);

        }

        catch(Exception ex)
        {
            btn_Send.Enabled = true;
            MessageBox.Show(ex.Message);

        }
    }

    public bool isValidEmail(string email)
    {
        bool flagFalse = false; ;
        if (!email.Contains('@'))
        {
            lbl_Error.Visible = true;
            lbl_Error.ForeColor = System.Drawing.Color.Red;
            lbl_Error.Text = "Email address must contain @";
            return flagFalse;
        }

        return true;
    }

}

【问题讨论】:

  • 我不确定我是否理解...您需要验证用于发送电子邮件的密码是否正确?为什么不尝试发送电子邮件并在失败时捕获异常?
  • 然后我会在设计上添加另一个标签,在文件后面的代码中,我会捕获错误并将其显示在标签中。这就是你给我的建议? -@jebar8
  • 查看我的回答了解更多详情。
  • 感谢您的回答,我意识到了错误,在密码凭据中,我必须取密码文本框中的值,而不是对密码进行硬编码。非常感谢 jebar :-)

标签: c# .net winforms visual-studio-2008


【解决方案1】:

假设您使用的是 Gmail,如您发布的屏幕截图所示,您无法在不尝试发送电子邮件的情况下检查密码。

我的建议是尝试发送电子邮件并在失败时捕获异常。然后,您可以在表单上显示一些错误指示,例如 MessageBox 或 Label。

请参阅SmtpClient 的文档。如果身份验证失败,Send 方法将抛出 SmtpException

编辑:

好吧,在看到您发布的附加代码之后,您已经在处理任何引发的异常,包括身份验证失败。如果密码不正确,用户将看到一个 MessageBox。

【讨论】:

    【解决方案2】:
     catch(Exception ex)
        {
            btn_Send.Enabled = true;
           // MessageBox.Show(ex.Message);
             lbl_Error.Text = "Invalid Username/Password";
    
        }
    

    【讨论】:

      【解决方案3】:

      SmtpException 进行尝试捕获,并向用户显示未经身份验证的信息。

      http://msdn.microsoft.com/en-us/library/h1s04he7.aspx

      您似乎在使用Windows Authentication.Active Directory 获取用户的电子邮件可能更容易,而不是提示输入凭据。

      How to obtain email address with window authentication

      【讨论】:

      • 非常感谢凯,我现在得到了解决方案,并意识到我的愚蠢错误:-)
      猜你喜欢
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2015-11-28
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      相关资源
      最近更新 更多