读书人

信箱激活注册信息

发布时间: 2013-01-23 10:44:50 作者: rapoo

邮箱激活,注册信息

----------region.aspx前台-------------
<body>
<form id="form1" runat="server">
<div>

<table style="width:50%;">
<tr>
<td>
用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
用户密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
邮箱地址:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="注册" onclick="Button1_Click" /></td>
<td>
 </td>
</tr>
</table>

</div>
</form>
</body>

--------region.aspx.cs后台-----------------

public void sendMail(string email, string activeCode,int id)
{

MailMessage msg = new MailMessage();

msg.From = new MailAddress("邮箱详细账号");
msg.To.Add(email);
msg.Subject = "请激活注册";

StringBuilder contentBuilder = new StringBuilder();
contentBuilder.Append("请单击以下连接完成激活!");
contentBuilder.Append("<a href='http://localhost:5566/CheckActiveCode.aspx?activecode=" + activeCode + "&id="+id+"'>激活</a>");


msg.Body = contentBuilder.ToString(); ;

msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient();
client.Host = "smtp.126.com";
client.Port = 25;

NetworkCredential credetial = new NetworkCredential();
credetial.UserName = "@之前的部分";
credetial.Password = "邮箱密码";

client.Credentials = credetial;

client.Send(msg);
}

protected void Button1_Click(object sender, EventArgs e)
{
string userName = this.TextBox1.Text;
string password = this.TextBox2.Text;
string email = this.TextBox3.Text;
string activeCode = Guid.NewGuid().ToString().Substring(0, 8);

string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
int number;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "insert into T_Users (UserName,Password,Email,Active,ActiveCode) values(@username,@password,@email,@active,@activecode) select @@identity";
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 = Convert.ToInt32(cmd.ExecuteScalar());
}
}
if (number > 0)
{
sendMail(email, activeCode,number);

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

---------CheckActiveCode.aspx.cs后台-------------------------------

protected void Page_Load(object sender, EventArgs e)
{

int id = Convert.ToInt32(Request["id"]);
string activeCode = Request["activecode"].ToString();


string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
int number;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select count(*) from T_Users 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;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select ActiveCode from T_Users 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 T_Users set Active=1 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("用户不存在,还没有注册成功!");
}
}

--------Web.config-----------------
<connectionStrings>
<add name="conStr" connectionString="data source=.;initial catalog=student;user id=sa;password=111111"/>
</connectionStrings>

读书人网 >移动开发

热点推荐