• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET结合数据库,发送邮件找回密码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 
 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net.Mail;
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MultiView1.ActiveViewIndex = 0;

}
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox2.Text.ToString();
string sql = "select count(*) from Usermember where username='" + name + "'";
int n=SqlDb.ExecScalar(sql);
if(n>0)
{
MultiView1.ActiveViewIndex=1;

string sql1 = "select pwdanswer,pwdquestion from Usermember where username='" + name + "'";
SqlDataReader dr = SqlDb.GetReader(sql1);
while (dr.Read())
{
TextBox3.Text = dr["pwdquestion"].ToString();

}
dr.Dispose();
}
else
{
MultiView1.ActiveViewIndex = 0;

Response.Write("<script>alert('无此用户名');history.back();</script>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox txt = new TextBox();

string an="";
string email = "";
string pwd = "";
Random pwd1 = new Random();
pwd = pwd1.Next(100000, 9999999).ToString();
txt.Text = pwd;
string name = TextBox2.Text.ToString();
string sql = "select pwdanswer,pwdquestion,user_email,userpwd from Usermember where username='" + name + "'";
SqlDataReader dr = SqlDb.GetReader(sql);
while (dr.Read())
{
an = dr["pwdanswer"].ToString();
email = dr["user_email"].ToString();
//pwd = dr["userpwd"].ToString();

}
if (TextBox4.Text == an)
{
string sql2 = "update Usermember set userpwd='" + SqlDb.MD5(txt) + "' where username='" + name + "'";
SqlDb.ExecSql(sql2);
MailAddress add=new MailAddress("[email protected]");
if (Send(add, email, "找回密码", pwd+" 请用此密码登录并及时修改您的密码!谢谢..."))
{
Response.Write("<script>alert('回答正确,一封邮件已发送到你的邮箱,请查 收!');location.href='" + Request.Url + "';</script>");
}
else
{
Response.Write("<script>alert('发送失败');JavaScript:history.back();</script>");
}
}
else
{
Response.Write("<script>alert('回答错误');JavaScript:history.back();</script>");
}

    }


/// <summary>
/// 发送电子邮件
/// </summary>
/// <param name="MessageFrom">发件人邮箱地址 </param>
/// <param name="MessageTo">收件人邮箱地址 </param>
/// <param name="MessageSubject">邮件主题 </param>
/// <param name="MessageBody">邮件内容 </param>
/// <returns> </returns>
public static bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
{
MailMessage message = new MailMessage();

        message.From = MessageFrom;
message.To.Add(MessageTo);              //收件人邮箱地址可以是多个以实现群发
message.Subject = MessageSubject;
message.Body = MessageBody;
message.IsBodyHtml = true;              //是否为html格式
message.Priority = MailPriority.High;   //发送邮件的优先等级

        SmtpClient sc = new SmtpClient();
sc.Host = "smtp.163.com";              //指定发送邮件的服务器地址或IP
sc.Port = 25;                           //指定发送邮件端口
sc.Credentials = new System.Net.NetworkCredential("[email protected]", "XXX"); //指定登录服务器的用户名和密码
try
{
sc.Send(message);       //发送邮件
}
catch (SmtpException ex)
{

//return ex.Message;
}
return true;


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
我对Asp.net页面一系列执行过程的认识发布时间:2022-07-10
下一篇:
asp.net—WebApi跨域发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap