註冊發送郵件~!

來源:互聯網
上載者:User

標籤:style   http   color   strong   ar   html   時間   amp   

  當使用者在一個新的網站上註冊使用者時,註冊成功後就會發送一個啟用連結,點擊啟用連結驗證通過後,使用者為啟用狀態,反之未啟用。

  建立一個使用者表【為了示範,使用者表欄位不是很多】

   Id int -主鍵ID

  UserName nvarchar--使用者名稱

  Password nvarchar --密碼

  Email nvarchar--註冊郵箱

 VCode[uniqueidentifier] ---啟用碼,

 Status nvarchar -- 啟用狀態

 ******************************************************

發送郵箱

public void SendValidataEmail(int userId)

{

   UserModel userinfo=GetModel(userId);

  userinfo.VCode=Guid.NewGuid();//產生啟用碼

  new UserManager().update(userinfo);//儲存更新--將啟用碼更新到當前註冊的使用者表

  

  MailMessage mailMsg=new MailMessage();

  mailMsg.From=new MailAddress("[email protected]","顯示名稱");//寄件者

  mailMsg.To.Add(new MailAddress(userinfo.Email,userinfo.UserName));//接收者

  mailMsg.Subject="啟用您在**新註冊的使用者"+user.UserName;//發送郵件的標題

  string  validateUrl="http://www.***.com/ValidateUser.aspx?username=" + HttpUtility.UrlEncode(user.UserName)+"&vcode="+userinfo.VCode;

  mailMsg.Body= "點擊下面的連結啟用您的帳戶(如果看不到超連結,則把網址粘貼到您的瀏覽器開啟):<a href=‘" + validateUrl + "‘>點此啟用</a>";

  mailMsg.IsBodyHtml = true;
    SmtpClient client = new SmtpClient("[email protected]");
    client.Credentials = new NetworkCredential("zhangsan", "123456");
    //有的smtp伺服器的使用者名稱是:[email protected],有的是zhangsan
     //使用者名稱、密碼必須和From一致

    client.Send(mailMsg);

}

---------------------------------------------------------------------------------------------------------

    public bool ValidateVCode(string username, Guid vcode)
        {
           //沒有做:如果使用者名稱不存在、啟用碼錯誤、啟用碼到期(增加一個啟用碼產生時間欄位,只有半個小時之內的才能有效)等幾個狀態定義為枚舉
           UserModel userInfo = GetByUserName(username);
            if (userInfo == null)
            {
                return false;
            }
            //return vcode == userInfo.VCode;
            if (vcode == userInfo.VCode)
            {
                //修改啟用狀態
                userInfo.Status = "已啟用";
                Update(userInfo);
                return true;
            }
            else
            {
                return false;
            }
        }

---------------------------------------------------------------------------------------------------------

註冊頁面-- RegisterUser.aspx

      UserModel user=new  UserModel();

      //暫且不考慮使用者名稱是否存,密碼加密,郵箱是否註冊過等驗證

    user.UserName=this.txtUserName.Text;      

    user.Password=this.txtPassword.Text; 

      user.Email=this.txtEmail.Text;

      user.Status="未啟用";

       new UserManager().Add(user);

       new UserManager().SendValidataEmail(user.id);

---------------------------------------------------------------------------------------------------------

驗證頁面--ValidateUser.aspx

        string username = Request["username"];
            Guid vCode = new Guid(Request["vcode"]);
            
            if (new UserManager().ValidateVCode(username, vCode))
            {
                labelMsg.Text = "啟用帳戶" + username + "成功";
            }
            else
            {
                labelMsg.Text = "啟用帳戶" + username + "失敗";
            }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.