实现用户注册时,向其邮箱发送激活码邮件,并进行状态处理
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>
相关文章推荐
- 写一个实现用户注册时,向其邮箱发送激活码邮件,并进行状态处理。
- 1.制作客户端邮件发送系统(winform版)实现用户注册时,向其油箱发送激活码邮件,并进行状态处理。
- 实现用户注册时,向其油箱发送激活码邮件,并进行状态处理
- 实现用户注册时,向其油箱发送激活码邮件,并进行状态处理
- 简单的实现用户注册时,向其油箱发送激活码邮件,并进行状态处理。 .
- 简单的实现用户注册时,向其油箱发送激活码邮件,并进行状态处理。
- asp.net实现一个用户进行注册的时候同时发送一个邮件到注册人的邮箱,通过发送的邮箱链接来激活该帐号
- asp.net实现一个用户进行注册的时候同时发送一个邮件到注册人的邮箱,通过发送的邮箱链接来激活该帐号
- asp.net发送邮件,使用MD5加密解密,实现用户注册完成后发送流水号(账号)到对方邮箱,jeasyUI验证,combobox的使用完成 实例下载
- spring-mybatis实现注册通过邮箱发送激活码激活注册用户
- python django 中利用QQ邮箱进行邮件发送,可用于邮箱注册、忘记密码
- Java实现注册邮箱激活验证 即邮件发送功能
- Exchange 2010 处于禁止发送用户自动收到来自IT的提醒邮件,而邮箱因为达到最大数据量被禁用的处理及对邮箱数据的清理!
- asp.net 实现邮箱验证(注册的时候发送邮件激活链接的那种)
- SAE平台搭建wordpress实现注册用户发送邮件
- Java-使用JavaMail发送一封邮件给用户邮箱功能实现
- SAE平台搭建wordpress实现注册用户发送邮件
- 实现注册时和绑定邮箱时发送邮件来确认绑定
- 发送邮件(注册用户并激活邮箱)
- java用户注册邮箱发送注册激活码