1

Yesterday, i had a task assigned by my senior to build a windows forms application in .net which looked like the image i attached. I did all the stuff regarding the sending process of the email application, but i stuck at one place, i couldn't figure out how to authenticate the password in the email form. The password must be of the same email, which was provided in the "From :" fields. This image displays the form which i designed

Here is the code behind of my form,

 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;
    }

}
codeLearner
  • 27
  • 2
  • 10
  • I'm not sure I understand... you need to verify that the password is correct for the email it is sending from? Why not just try and send the email and catch an Exception if it fails? – jebar8 Mar 07 '13 at 05:05
  • and then i would add another label on the design, and in the code behind file, i would catch the error and display it in the label. Thats what you are advising me? -@jebar8 – codeLearner Mar 07 '13 at 05:08
  • See my answer for additional details. – jebar8 Mar 07 '13 at 05:20
  • Thanks for your answer, and i realized the mistake, in the password credentials, i must take the value in the password textbox, rather than hard coding the password. thanks a lot jebar :-) – codeLearner Mar 07 '13 at 05:31

3 Answers3

1

Assuming you're using Gmail like the screenshot you posted shows, you can't check the password without trying to send the email.

My advice would be to attempt to send the email and catch an Exception if it fails. You can then show some indication that there has been an error, like a MessageBox or a Label on your form.

See the documentation for SmtpClient. The Send methods will throw an SmtpException if authentication fails.

EDIT:

Well, after seeing the additional code you posted, you are already handling any exceptions that are thrown, including an authentication failure. The user will see a MessageBox if the password is incorrect.

jebar8
  • 2,113
  • 3
  • 21
  • 31
0

Do a try catch for SmtpException and display the information to the user is unauthenticated.

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

It looks like your using Windows Authentication. It might be easier to fetch the user's email from Active Directory rather then prompting for credentials.

How to obtain email address with window authentication

Community
  • 1
  • 1
Kye
  • 5,919
  • 10
  • 49
  • 84
0
 catch(Exception ex)
    {
        btn_Send.Enabled = true;
       // MessageBox.Show(ex.Message);
         lbl_Error.Text = "Invalid Username/Password";

    }