忘记密码的代码,电子邮件未发送出去。 C# [英] Code for forgot password, email not sent out. C#

查看:87
本文介绍了忘记密码的代码,电子邮件未发送出去。 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Configuration;

public partial class ForgotPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try {
            DataSet ds = new DataSet();
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT UserName, Password FROM Users Where Email='" + txtEmail.Text.Trim() + "'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }
            if (ds.Tables[0].Rows.Count>0)
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(txtEmail.Text);
                msg.To.Add(txtEmail.Text);
                msg.Subject="Share It: Password Recovery";
                msg.Body="Hi, " + ds.Tables[0].Rows[0]["FirstName"] +" " + ds.Tables[0].Rows[0]["LastName"] +"<br />As requested, here is your login details<br /><br />Your Username: " + ds.Tables[0].Rows[0]["UserName"] + "<br /><br />Your Password: " + ds.Tables[0].Rows[0]["Password"] + "<br /><br />Thank you for trusting us! Hope to hear from you soon!<br /><br />";
                msg.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient("smtp.gamil.com", 587);
                smtp.UseDefaultCredentials = false;
                //smtp.Host = "smtp.gmail.com";
                //smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("theresakang93@gmail.com", "passwordremoved");
                smtp.EnableSsl = true;
                smtp.Send(msg);
                lbltxt.Text = "Your Password Details Sent to your mail.";
            }
        
            else
            
                lbltxt.Text = "The Email you entered does not exists.";
        }    
        catch (Exception ex) {
        
                Console.WriteLine("{0} Exception caught.", ex);
                lbltxt.Text = "Unable to send";
            }

推荐答案

看过这段代码后,由于所有安全漏洞,我绝不会使用你的网站。 />


但是,如果没有发送电子邮件,那么您的邮件服务器设置不正确,而不是这段代码有多糟糕..



现在,密码被加密存储,并且无法向他人发送密码,通常,您只能发送使用GUID与用户关联的重置链接,或者他们下次登录时需要更改的临时密码。
Having seen this code, I would never use your website, due to all the security holes.

But, if the email is not sent, then your mail server is not set up correctly, not a big leap from how bad this code is.

Nowadays, passwords get stored encrypted, and it is impossible to send someone their password, all you can do, generally, is send a reset link that uses a GUID to associate with a user, or a temp password they need to change the next time they log in.


请参阅我的回答 - 从asp.net发送电子邮件到gmail [ ^ ]。

这个效果非常好。经过测试。



如果你得到任何例外,请回复我。我会帮忙。
Refer my answer - sending email to gmail from asp.net[^].
This works pretty good. Tested.

If you are getting any Exception, please reply me with that. I will, help.


请看问题的评论,Ron和我的。



请查看我过去的答案,了解更多细节:

我已经加密了我的密码,但是当我登录时它给了我一个错误。如何解密 [ ^ ] ,

解密加密密码 [ ^ ],

存储密码值int sql server with secure方式 [ ^ ]。



-SA
Please see the comments to the questions, Ron's and mine.

Please see my past answers for some further detail:
i already encrypt my password but when i log in it gives me an error. how can decrypte it[^],
Decryption of Encrypted Password[^],
storing password value int sql server with secure way[^].

—SA


这篇关于忘记密码的代码,电子邮件未发送出去。 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆