0

I'm trying to write code to send a forgotten password via email, so when the user clicks on the "Forget Password" button it should be to send the password to their email ... but it give this error

Server Error in '/' Application. The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

Source Error:

Line 50: smtp.Send(ms);

The source code here:

       protected void Buttonpsw_Click(object sender, EventArgs e)
         {
        string mainconn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
        SqlConnection con = new SqlConnection(mainconn);
        string sqlquery = "select Email,Password1 from [dbo].[Users1] where Email =@Email";
        SqlCommand sqlcommand = new SqlCommand(sqlquery, con);
        sqlcommand.Parameters.AddWithValue("@Email",Textemail.Text);
        con.Open();
        SqlDataReader sdr = sqlcommand.ExecuteReader();
        if (sdr.Read())
        {

            string username = sdr["Email"].ToString();
            string Password = sdr["Password1"].ToString();
            MailMessage ms = new MailMessage("roqaia.alrfou@gmail.com",Textemail.Text);
            ms.Subject = "Your password!";
            ms.Body = string.Format("Hello : <h1>{0}</h1> is your Email ID <br/> Your Password is <h1>{1}</h1>",username,Password);
            ms.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential nc = new NetworkCredential();
            nc.UserName = "roqaia.alrfou@gmail.com";
            nc.Password = "133";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = nc;
            smtp.Port = 587;
            smtp.Send(ms);
            Labelmsg.Text = "Your Password has been sent to" + Textemail.Text;
            Labelmsg.ForeColor = Color.Green;


        }
        else
        {
            Labelmsg.Text = Textemail.Text+" This Email ID isn't Exist! Please sign up";
            Labelmsg.ForeColor = Color.Red;

        }

And the connectionstring in web config has this code: (Tracking_System is my database name)

    <connectionStrings>
        <add name="dbconnection"
             connectionString="server= USER-PC\SQL;Data 
   Source=localhost;Initial Catalog=Tracking_System;
   Integrated Security=True;Pooling=False
   ; Trusted_Connection=True;"
         providerName="System.data.sqlclient" />
</connectionStrings>

I tried to ping smtp on command prompt it give this:

C:\WINDOWS\system32>ping smtp.google.com Ping request could not find host smtp.google.com. Please check the name and try again.

C:\WINDOWS\system32>ping smtp.gmail.com

Pinging gmail-smtp-msa.l.google.com [64.233.184.108] with 32 bytes of data: Reply from 64.233.184.108: bytes=32 time=85ms TTL=37 Reply from 64.233.184.108: bytes=32 time=91ms TTL=37 Reply from 64.233.184.108: bytes=32 time=86ms TTL=37 Reply from 64.233.184.108: bytes=32 time=85ms TTL=37

Ping statistics for 64.233.184.108: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 85ms, Maximum = 91ms, Average = 86ms

Sagar Shirke
  • 648
  • 9
  • 32

2 Answers2

0

This generally happens when you try login from different time zone or IP Address Computer. Your production server and the mail id you have used both are in different time zone. Choose any of the one solution:

1) Log in to production server via remote access, and sign in to gmail once with your credentials. They will ask for the confirmation, confirm it and log out.

Or 2) log in gmail to your local computer, Follow this Link and choose review this activity and take proper actions.

I found this over this link Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

Gaurav
  • 782
  • 5
  • 12
  • Gaurav Thank you sir, i tried that .it did not work! I think the problem in the connectionstring in the web.config code – roqaia.alrfou Jan 17 '19 at 07:14
0

The problem was my password of my email is wrong, nc.Password = "133"; I was think the correct password is from the database not the real password of my email. So The above code is correct 100% .Hopefully someone need it