简体   繁体   English

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

[英]Send forget password to email Asp.net code error

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. SMTP 服务器需要安全连接或客户端未通过身份验证。 The server response was: 5.5.1 Authentication Required.服务器响应为:5.5.1 需要身份验证。 Learn more at Description: An unhandled exception occurred during the execution of the current web request.在描述中了解更多信息:在执行当前 Web 请求期间发生未处理的异常。 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.异常详细信息:System.Net.Mail.SmtpException:SMTP 服务器需要安全连接或客户端未通过身份验证。 The server response was: 5.5.1 Authentication Required.服务器响应为:5.5.1 需要身份验证。 Learn more at了解更多信息

Source Error:源错误:

Line 50: smtp.Send(ms);第 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 ) 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>

I tried to ping smtp on command prompt it give this:我试图在命令提示符下 ping smtp 它给出了这个:

C:\\WINDOWS\\system32>ping smtp.google.com Ping request could not find host smtp.google.com. C:\\WINDOWS\\system32>ping smtp.google.com Ping 请求找不到主机 smtp.google.com。 Please check the name and try again.请检查名称并重试。

C:\\WINDOWS\\system32>ping smtp.gmail.com 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 gmail-smtp-msa.l.google.com [64.233.184.108] 有 32 个字节的数据: 来自 64.233.184.108 的回复:bytes=32 时间=85ms TTL=37 来自 64.233.184.108 的回复时间=9 bytes33ms TTL=37 来自 64.233.184.108 的回复:bytes=32 时间=86ms TTL=37 来自 64.233.184.108 的回复:bytes=32 时间=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 64.233.184.108 的 Ping 统计数据:数据包:发送 = 4,接收 = 4,丢失 = 0(丢失 0%),以毫秒为单位的近似往返时间:最小值 = 85 毫秒,最大值 = 91 毫秒,平均值 = 86 毫秒

This generally happens when you try login from different time zone or IP Address Computer.当您尝试从不同的时区或 IP 地址计算机登录时,通常会发生这种情况。 Your production server and the mail id you have used both are in different time zone.您的生产服务器和您使用的邮件 ID 位于不同的时区。 Choose any of the one solution:选择以下任一解决方案:

1) Log in to production server via remote access, and sign in to gmail once with your credentials. 1) 通过远程访问登录到生产服务器,并使用您的凭据登录一次 gmail。 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.或 2) 将 gmail 登录到您的本地计算机,点击此链接并选择查看此活动并采取适当的措施。

I found this over this link Gmail Error :The SMTP server requires a secure connection or the client was not authenticated.我通过此链接找到了Gmail 错误:SMTP 服务器需要安全连接或客户端未通过身份验证。 The server response was: 5.5.1 Authentication Required 服务器响应为:5.5.1 Authentication Required

The problem was my password of my email is wrong, nc.Password = "133";问题是我的邮箱密码错误, 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所以上面的代码是 100% 正确的。希望有人需要它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM