數據庫 表的設計
State為用戶狀態 0為禁用 1為可用 默認為0,下面有個UserGUID,這個字段將來用於激活賬戶
首先你要寫一個表單,驗證碼神馬的,這個我就不寫了。。直接寫處理的 代碼在下面
if (IsPostBack) 2 { 3 string userName = Request["UserName"]; 4 string userPwd = Request["UserPwd"]; 5 string userEmail = Request["UserEmail"]; 6 string EmailGUID = Guid.NewGuid().ToString(); 7 8 //UserInfosEntities 9 if (CheckValidCode()) 10 { 11 if (InsertUserInfo(new userinfo() 12 { 13 UserName = userName, 14 UserPwd = userPwd, 15 State = 0, 16 UserEmail = userEmail, 17 UserGUID = EmailGUID 18 19 })) 20 { 21 22 string str = Request.ServerVariables["Http_Host"]; //這句話的意思是獲取域名和端口,因為我是在本機調試的,要是重新生成的話端口就改了 - - 我很郁悶 ...這是大神告訴我的... 23 MailMessage mailMsg = new MailMessage(); //要引入System.Net這個Assembly 24 mailMsg.From = new MailAddress("[email protected]", "自己的名字"); //源郵件地址 25 mailMsg.To.Add(new MailAddress(userEmail, "對方的名字")); //目的郵件地址。可以有多個收件人 26 mailMsg.Subject = "激活帳號"; //發送郵件的標題 27 userName = Common.Base64.EncodeBase64(Encoding.UTF8, userName); //這個是把傳去的名字轉換成base64的,我試過Encoding,不行,找了好久,中文一直亂碼,只好把它轉成這個樣子了。。 28 string emailStr = string.Format("單擊以下激活鏈接,激活帳號http://{0}/ActivUserInfo.aspx?UserName={1}&GUID={2}",str,userName,EmailGUID); //這個就是將來發到郵箱裡面的激活鏈接 29 mailMsg.Body = emailStr; //發送郵件的內容 30 mailMsg.IsBodyHtml = true; //內容是否是HTML 31 mailMsg.BodyEncoding = Encoding.UTF8; //編碼格式為UTF-8 32 SmtpClient client = new SmtpClient("smtp.qq.com"); // 發件人所使用郵箱的SMTP服務器地址。 33 client.Credentials = new NetworkCredential("發送郵件的帳號", "發送郵件的密碼"); //發件人郵箱的用戶名和密碼. 34 client.Send(mailMsg); //發送郵件 35 Response.Redirect("/Admin.aspx"); 36 37 } 38 else 39 { 40 Response.Redirect("/Login.aspx"); 41 42 } 43 44 45 } 46 else 47 { 48 49 Message = "驗證碼輸入錯誤,請重新輸入!!!"; 50 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 9 namespace WebDemoUserInfo 10 { 11 public partial class ActivUserInfo : System.Web.UI.Page 12 { 13 public string ActiveMessage { get; set; } 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 string userName = Common.Base64.DecodeBase64(Encoding.UTF8, Request["UserName"]); //把傳過來的UserName解密,Base64的代碼在後面 17 string gUid = Request["GUID"]; // 18 if (userName != null && gUid != null) 19 { 20 int result = CheckUserInfo(userName, gUid); 21 switch (result) 22 { 23 case 0: 24 ActiveMessage = "激活失敗"; 25 break; 26 case 1: 27 ActiveMessage = "激活成功"; 28 break; 29 case 2: 30 ActiveMessage = "不能重復激活!!!"; 31 break; 32 defalut: ActiveMessage = "未知錯誤,請聯系系統管理員!!!"; 33 } 34 } 35 36 37 } 38 39 private int CheckUserInfo(string userName, string gUID) 40 { 41 try 42 { 43 var db = new UserInfosEntities(); 44 if (db.userinfo.Count(i => i.UserName == userName && i.UserGUID == gUID) == 1) 45 { 46 var model = db.userinfo.FirstOrDefault(i => i.UserGUID == gUID); 47 if (model != null && model.State == 0) 48 { 49 model.State = 1; 50 51 } 52 else 53 { 54 return 2; 55 } 56 return db.SaveChanges() == 1 ? 1 : 0; 57 } 58 else 59 { 60 return 0; 61 } 62 } 63 finally 64 { 65 Dispose(); 66 } 67 } 68 } 69 }1 public static class Base64 2 { 3 public static string EncodeBase64(Encoding encoding, string source) 4 { 5 byte[] bytes = encoding.GetBytes(source); 6 return Convert.ToBase64String(bytes); 7 } 8 9 public static string DecodeBase64(Encoding encoding, string result) 10 { 11 byte[] bytes = Convert.FromBase64String(result); 12 return encoding.GetString(bytes); 13 } 14 } Base64 加解密
整天沒事自己研究...覺得還有好多要學....加油...
hi.baidu.com/...3.html
簡單明了,也有注釋,你看一下,希望能幫到你
我告訴你
你需要一個字段來存儲這個用戶的驗證號
然後再注冊的時候
int checkNumber = ran.Next(10000, 99999); 生成一個隨機嗎 存到數據庫
然後發送郵件 如下
string smtpServer = 地址
int smtpPort = 端口
string userAccount = 帳號
string userPassword = 密碼
string userName = 姓名
string EmailAddress = EMAIL地址
System.Net.Mail.SmtpClient client = new SmtpClient(smtpServer, smtpPort);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(userAccount, userPassword);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailAddress fromEmal = new MailAddress(EmailAddress, userName);
MailAddress toEmail = new MailAddress(strto);
System.Net.Mail.MailMessage message = new MailMessage(fromEmal, toEmail);
message.Subject = strSubject;
message.Body = strBody;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);