通過郵箱驗證註冊——.net代碼

來源:互聯網
上載者:User

標籤:

      在寫一些面向使用者的網站類的程式時,必不可少的一個就是註冊,通常情況下,我們會選擇郵箱驗證後註冊,或者手機發送驗證碼註冊。上篇文章中已經簡單的描述了手機驗證註冊,這篇主要介紹一下郵箱驗證。

      郵箱驗證的步驟主要是:使用者填寫郵箱帳號並提交、後台判斷輸入是否是有效郵箱,如果為有效郵箱,發送驗證鏈結接到輸入的郵箱中,使用者通過郵箱的連結,成功註冊。

      想實現這個功能,首先,在填寫註冊資訊時,後台產生一個預設的狀態(例如:state=0,表示未驗證)存入資料庫。提交表單後,把輸入的內容存入資料庫,然後通過send命令,向指定郵箱發送驗證鏈結接,使用者通過點選連結,是預設狀態更改(例如:state=1,表示驗證通過),註冊既成功。

      發送郵件的代碼如下:

 protected bool sendTheMail(string smtpserver, string smptport, string userName, string pwd, string strfrom, string strto, string subj, string bodys)        {            SmtpClient _smtpClient = new SmtpClient();            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定電子郵件發送方式            _smtpClient.Host = smtpserver;//指定SMTP伺服器            if (YXShop.Common.WebUtility.isNumeric(smptport))            {                int port = Convert.ToInt32(smptport);                if (port > 0)                    _smtpClient.Port = port;            }            _smtpClient.Credentials = new System.Net.NetworkCredential(userName,pwd);//使用者名稱和密碼            MailMessage _mailMessage = new MailMessage(strfrom, strto);            _mailMessage.Subject = subj;//主題            _mailMessage.Body = bodys;//內容            _mailMessage.BodyEncoding = System.Text.Encoding.Default;//本文編碼            _mailMessage.IsBodyHtml = true;//設定為HTML格式            _mailMessage.Priority = MailPriority.High;//優先順序            try            {                _smtpClient.Send(_mailMessage);                return true;            }            catch (Exception e)            {                throw e;            }        }        private bool SendTEmail(string strto, string subj, string bodys)        {            userReg_Accessor target = new userReg_Accessor(); // TODO: 初始化為適當的值            string smtpServer = "smtp.163.com"; // TODO: 初始化為適當的值            string smptport = "25"; // TODO: 初始化為適當的值            string userName = "**@163.com"; // TODO: 初始化為適當的值            string pwd = "**"; // TODO: 初始化為適當的值            string strFrom = "**.com"; // TODO: 初始化為適當的值             strto = "**.com"; // TODO: 初始化為適當的值             subj = "hello Miss lu"; // TODO: 初始化為適當的值             bodys = "----------"; // TODO: 初始化為適當的值            bool bl = sendTheMail(smtpServer, smptport, userName, pwd, strFrom, strto, subj, bodys);            return bl;        }
      先說說這六個參數,smtpSever是郵件伺服器,如果為smtp.163.com 表示網易郵箱伺服器,如果是qq郵箱,則為smtp.qq.com,如果是其他的郵箱,可以查詢網路。smptport是連接埠號碼,通常情況下網易郵箱和qq郵箱發送端連接埠號碼都是25,userName是發送端帳號,通常為發送郵箱帳號,與strFrom相同。pwd為發送端郵箱密碼,strto為註冊的郵箱,即輸入的有效郵箱帳號。subj是發送的郵件的主題,bodys是發送的郵件內文。

      如果所有配置都正確,調用send方法,即可發送郵件。在郵件內文中做好設定,即可實現通過郵箱註冊的功能。

      簡單總結如上,如有錯誤,歡迎指出,不勝感激!

通過郵箱驗證註冊——.net代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.