您的位置:首页 > 其它

实现用户注册时,向其邮箱发送激活码邮件,并进行状态处理

2013-01-04 16:58 846 查看


==========================================================

web页的设计:

<configuration>

<system.web>

<compilation debug="true" targetFramework="4.0" />

</system.web>

<connectionStrings>

<add name="studentconStr" connectionString="Data Source=.;Initial Catalog=数据库名;User ID=连接数据库账号;Password=连接数据库密码"/> </connectionStrings> </configuration>

===========================================================

regin.aspx页的设计:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

<style type="text/css">

.style1 { width: 237px; }

.style2 { width: 91px; }

</style> </head>

<body> <

form id="form1" runat="server">

<div style="background-color:#eeeeee">

<table style="width:50%;">

<tr> <td class="style1">用户名:</td>

<td class="style2"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td></tr>

<tr> <td class="style1">密码:</td>

<td class="style2"> <asp:TextBox ID="TextBox2" runat="server" style="margin-left: 0px"></asp:TextBox></td></tr>

<tr><td class="style1"> 邮箱地址:</td>

<td class="style2"><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td></tr>

<tr><td class="style1"><asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /></td><td class="style2"> </td></tr>

</table></div></form>

</body> </html>

================================================================

regin.aspx.cs页的设计:

namespace 激活注册

{

public partial class regin : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

public void sendMail(string email, string activeCode)

{

MailMessage mailMsg = new MailMessage();

mailMsg.From = new MailAddress("126邮箱的账号");

mailMsg.To.Add(email);

mailMsg.Subject = "请激活注册";
StringBuilder contentBuilder = new StringBuilder();

contentBuilder.Append("请单击以下链接完成激活");

contentBuilder.Append("<a href='CheckActiveCode.aspx ?activecode=" + activeCode + "&id=8'>激活</a>");

mailMsg.Body = contentBuilder.ToString();//拼接字符串

mailMsg.IsBodyHtml = true;

SmtpClient client = new SmtpClient(); //发件方服务器地址

client.Host = "smtp.126.com";

NetworkCredential credetial = new NetworkCredential();

credetial.UserName = "126邮箱的账号";

credetial.Password = "126邮箱的密码";

client.Credentials = credetial;

client.Send(mailMsg); }

protected void Button1_Click(object sender, EventArgs e)

{

string username = this.TextBox1.Text;

string password = this.TextBox2.Text;

string email = this.TextBox3.Text;

//生成激活码,Guid为唯一标示符

string activeCode = Guid.NewGuid().ToString().Substring(0, 8);//生成激活码

string conStr = ConfigurationManager.ConnectionStrings["studentconStr"].ConnectionString;

int number;

using (SqlConnection con = new SqlConnection(conStr))

{

string sql = "insert into 数据库名 (UserName,PassWord,Email,Active,ActiveCode) values(@username,@password,@email,@active,@activecode)"; SqlParameter[] prams=new SqlParameter[]{

new SqlParameter("@username",username),

new SqlParameter("@password",password),

new SqlParameter("@email",email),

new SqlParameter("@active",false),

new SqlParameter("@activecode",activeCode) };

using (SqlCommand cmd = new SqlCommand(sql, con))

{ con.Open();

cmd.Parameters.AddRange(prams);

number=cmd.ExecuteNonQuery(); } }

if (number > 0) {

sendMail(email, activeCode);//给注册用户发邮件

Response.Redirect("reginMessage.aspx"); }

else { Response.Write("注册失败,请重新注册!");

}

}

} }

========================================================================

CheckActiveCode.aspx.cs设计:

protected void Page_Load(object sender, EventArgs e)

{ //1、取出参数id

int id = Convert.ToInt32(Request["id"]);

string activeCode = Request["activecode"].ToString();

//2、判断id为id的记录是否存在 //连接数据库

string conStr = ConfigurationManager.ConnectionStrings["studentconStr"].ConnectionString;

int number;

using (SqlConnection con = new SqlConnection(conStr))

{ string sql = "select count(*) from 数据库名 where id=@id";

using (SqlCommand cmd = new SqlCommand(sql, con))

{ con.Open();

cmd.Parameters.AddWithValue("@id", id);

number = Convert.ToInt32(cmd.ExecuteScalar()); } }

if (number > 0) {

string AC; //如果该用户存在取出器ActiveCode字段进行比较,如果一样,把Active字段改为true

using (SqlConnection con = new SqlConnection(conStr))

{

string sql = "select ActiveCode from 数据库名 where id=@id";

using (SqlCommand cmd = new SqlCommand(sql, con))

{ con.Open();

cmd.Parameters.AddWithValue("@id", id);

AC = cmd.ExecuteScalar().ToString(); } }

if (activeCode == AC) {

Response.Write("激活成功");

using (SqlConnection con = new SqlConnection(conStr)) {

string sql = "update 数据库名 set Active=true where id=@id";

using (SqlCommand cmd = new SqlCommand(sql, con)) {

con.Open();

cmd.Parameters.AddWithValue("@id", id);

number = Convert.ToInt32(cmd.ExecuteScalar());

} } }

else {

Response.Write("用户名已存在,但激活验证码错误"); } }

else { Response.Write("用户名不存在,还没注册成功"); } }

=======================================================================

reginMessage.aspx设计:

<body>

<form id="form1" runat="server"> <div>

恭喜您,注册成功!请激活! </div>

</form> </body>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐