写一个实现用户注册时,向其邮箱发送激活码邮件,并进行状态处理。
2013-01-04 16:36
721 查看
==========================================================
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实现一个用户进行注册的时候同时发送一个邮件到注册人的邮箱,通过发送的邮箱链接来激活该帐号
- spring-mybatis实现注册通过邮箱发送激活码激活注册用户
- asp.net发送邮件,使用MD5加密解密,实现用户注册完成后发送流水号(账号)到对方邮箱,jeasyUI验证,combobox的使用完成 实例下载
- 4.设计一个Email邮箱注册应用程序。要求:用户输入完成单击“立即注册”按,判断“密码”和“确认密码”文本框内容是否一致,如果一致在立即注册按钮上方显示用户输入的邮件地址,运行结果如图所示。
- exchange中实现一个用户邮箱拥有多个邮件地址
- exchange中实现一个用户邮箱拥有多个邮件地址
- [经验]同时发送邮件会当成垃圾邮件 ecshop 邮箱设置 用户注册邮箱验证.txt
- SAE平台搭建wordpress实现注册用户发送邮件
- 制作一个注册网页(2)并实现自动发送邮箱
- Java实现注册邮箱激活验证 即邮件发送功能
- asp.net 实现邮箱验证(注册的时候发送邮件激活链接的那种)
- 一个发送邮件功能,用户反映没有收到邮件,如何处理bug
- Exchange 2010 处于禁止发送用户自动收到来自IT的提醒邮件,而邮箱因为达到最大数据量被禁用的处理及对邮箱数据的清理!