第一步:首先新建一個頁面register頁面,在該頁面的前台添加上用戶名、密碼、郵箱地址和三個文本框以及注冊按鈕。
第二步:在register頁面的register.aspx.cs頁面加上以下代碼:
public void sendMail(string email, string activeCode,int number)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("q******09@126.com");////填寫一個126的郵箱
msg.To.Add(email);
msg.Subject = "請激活注冊";
StringBuilder contentBuilder = new StringBuilder();
contentBuilder.Append("請單擊以下連接完成激活!");
contentBuilder.Append("<a href='http://localhost:8899/CheckActiveCode.aspx?activecode="+activeCode+"&id="+number+"'>激活</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 = "qzc900809";
credetial.Password = "900809";
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;//在web.Confing中設置連接數據庫字符串
int number;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "insert into email (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);
//sendMail(string email,string activeCode,int id);//給注冊用戶發郵件
Response.Redirect("regionMessage.aspx");///////這個可有可無,只是一個提示成功的頁面
}
else
{
Response.Write("注冊失敗,請重新注冊!");
}
}
第四步:再重新新建一個CheckActiveCode.aspx頁面,然后再該頁面(CheckActiveCode.aspx.cs)的Page_Load中
加上以下代碼:
//1取出參數id
int id = Convert.ToInt32(Request["zcid"]);
string activeCode = Request["activecode"].ToString();
//2判斷id為id的記錄是否存在。
//連接數據庫
string conStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
int number;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select count(*) from Registor where zcid=@id";
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddWithValue("@id",id);
number=Convert.ToInt32(cmd.ExecuteScalar());
}
}
if (number > 0)
{
//如果該用戶存在取出其ActiveCode字段進行比較。如果一樣。把Ative字段修改為true
//連接數據庫
string AC;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select ActiveCode from Registor where zcid=@id";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", id);
AC=cmd.ExecuteScalar().ToString();//wrong?
}
}
if (activeCode == AC)
{
Response.Write("激活成功!");
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "update Registor set Active=1 where zcid=@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("用戶不存在,還沒有注冊成功!");
}
}
第五步:這樣完成之后,就可以去自己剛才填寫的126郵箱中查看郵件,打開郵箱單擊激活,就激活成功了,
然后再去數據庫中查看Active的狀態,此時應該是True,這樣,一個完整的注冊並發送郵件加激活的功能就完成了。
