将忘记密码发送到电子邮件 Asp.net 代码错误

Send forget password to email Asp.net code error

我正在尝试编写代码以通过电子邮件发送忘记的密码,因此当用户单击 "Forget Password" 按钮时,应该将密码发送到他们的电子邮件中……但它给出了这个错误

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

源代码在这里:

       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;

        }

Web 配置中的连接字符串有这样的代码:(Tracking_System 是我的数据库名称

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

我尝试在命令提示符下 ping smtp,结果如下:

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

当您尝试从不同时区或 IP 地址计算机登录时,通常会发生这种情况。您的生产服务器和您使用的邮件 ID 都在不同的时区。选择一种解决方案:

1) 通过远程访问登录到生产服务器,并使用您的凭据登录一次 gmail。他们会要求确认,确认并注销。

或 2) 将 gmail 登录到您的本地计算机,按照此 Link and choose review this activity 并采取适当的措施。

我在上面找到了这个 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

问题是我的电子邮件的 密码 错误,nc.Password = "133"; 我认为正确的密码来自数据库而不是我电子邮件的真实密码。 所以上面的代码是 100% 正确的。希望有人需要它