asp.net发送邮件找回密码的示例代码
本文导语: asp.net实现找回密码功能,代码: 代码示例: 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;...
asp.net实现找回密码功能,代码:
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("alert('无此用户名');history.back();");
}
}
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("");
if (Send(add, email, "找回密码", pwd+" 请用此密码登录并及时修改您的密码!谢谢..."))
{
Response.Write("alert('回答正确,一封邮件已发送到你的邮箱,请查 收!');location.href='" + Request.Url + "';");
}
else
{
Response.Write("alert('发送失败');JavaScript:history.back();");
}
}
else
{
Response.Write("alert('回答错误');JavaScript:history.back();");
}
}
///
/// 发送email邮件,找回密码
///
/// 发件人邮箱地址
/// 收件人邮箱地址
/// 邮件主题
/// 邮件内容
///
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("", "XXX"); //指定登录服务器的用户名和密码
try
{
sc.Send(message); //发送邮件
}
catch (SmtpException ex)
{
//return ex.Message;
}
return true;
}
}