Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi When I run this code I've this problem: (The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.)
So, what should I do to solve it?

What I have tried:

C#
protected void btPassRec_Click(object sender, EventArgs e)
    {
        String CS = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("select * from users where Email='"+tbEmailId.Text+"'",con);
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            if (dt.Rows.Count != 0)
            {
                String myGUID = Guid.NewGuid().ToString();
                int Uid = Convert.ToInt32(dt.Rows[0][0]);
                SqlCommand cmd1 = new SqlCommand("insert into ForgotPassRequests values('"+myGUID+"','"+Uid+"',getdate())",con);
                cmd1.ExecuteNonQuery();

                //send email
                String ToEmailAddress = dt.Rows[0][3].ToString();
                String Username = dt.Rows[0][1].ToString();
                String EmailBody = "Hi "+Username+ ",<br /><br /> Click the link below to reset your password <br /><br /> http://localhost:48599/RecoverPassword.aspx?Uid="+myGUID;
                MailMessage PassRecMail = new MailMessage("youremail@gmail.com", ToEmailAddress);
                PassRecMail.Body = EmailBody;
                PassRecMail.IsBodyHtml = true;
                PassRecMail.Subject = "Reset Password";

                //SmtpClient SMTP = new SmtpClient("smtp-mail.outlook.com", 587);
                SmtpClient SMTP = new SmtpClient("smtp.gmail.com", 587);
                SMTP.Credentials = new NetworkCredential()
                {
                    UserName = "youremail@gmail.com",
                    Password = "youremailpassword"      //https://www.youtube.com/watch?v=i6kXgf2MBl8
                };
                SMTP.EnableSsl = true;
                SMTP.Send(PassRecMail);

                lblPassRec.Text = "Check your email to reset your password.";
                lblPassRec.ForeColor = Color.Green;
                
            }
            else
            {
                lblPassRec.Text = "OOps This email id DOES NOT exist in our database !";
                lblPassRec.ForeColor = Color.Red;
            }
        }
Posted
Updated 7-May-16 4:49am
v2
Comments
Richard MacCutchan 7-May-16 4:59am    
You should do what the message tells you and check that the userid is valid on gmail.

1 solution

Take care of the suggestions given in comments. I think you are missing the below code in mail sending block.
C#
client.UseDefaultCredentials = false;

Refer my past answer - sending email to gmail from asp.net[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900