.NET下用C#實現郵箱啟用功能

來源:互聯網
上載者:User

標籤:

  最近要用到安全郵箱啟用的功能,故寫篇部落格記錄下。

  思路:在表中增加一個欄位State來記錄郵箱是否啟用(0啟用,1未啟用。)

1、發送郵件。    1-1,給郵箱發送郵件。內容:啟用地址+GUID。    1-2,以GUID作為鍵Userid為值存入Session中2、點擊郵箱裡啟用連結 啟用。    2-1,請求啟用頁面。(需在20分鐘內,同一瀏覽器開啟)
    2-2,通過GUID取Session中的Userid  進行啟用。 上代碼: 1.發送郵件

  我們可以使用.net內建的類庫:System.Net.Mail;

 1         /// <summary> 2         /// 發送啟用連結. 3         /// </summary> 4         public static void SendEmail(string activeCode, string mail) 5         { 6             MailMessage mailMsg = new MailMessage();//兩個類,別混了,要引入System.Net這個Assembly 7             mailMsg.From = new MailAddress("[email protected]");//源郵件地址 ,寄件者 8             mailMsg.To.Add(new MailAddress(mail));//目的郵件地址。可以有多個收件者. 9             mailMsg.Subject = "請啟用在商城中的註冊連結";//發送郵件的標題 10             mailMsg.Body = "<a href=‘http://localhost:2934/Home/Active/?userId=36&activeCode=" + activeCode + "‘>請單擊啟用</a>";//發送郵件的內容 11             mailMsg.IsBodyHtml = true;12             SmtpClient client = new SmtpClient("smtp.163.com");//smtp.163.com,smtp.qq.com,寄件者使用的郵箱的SMTP伺服器。13             client.Credentials = new NetworkCredential("[email protected]", "xxxxxxxx");//指定寄件者的郵箱的帳號與密碼.14             client.Send(mailMsg);//排隊發送郵件.15 16         }    
View Code

 

  發送郵件服務端代碼

 

 1  public partial class Index : System.Web.UI.Page 2     { 3         public string Url { get; set; } 4         protected void Page_Load(object sender, EventArgs e) 5         { 6  7  8             string key = Guid.NewGuid().ToString("N"); 9             // 以GUID為key 儲存使用者id10             Session[key] = 1;11             // Url需要發送至郵件12             Url = "http://localhost:25088/active.aspx?key=" + key; 13             14         }15     }
View Code

 

2.啟用

 

 1   public partial class active : System.Web.UI.Page 2     { 3         public string Msg { get; set; } 4         protected void Page_Load(object sender, EventArgs e) 5         { 6             Msg = "啟用失敗"; 7             // 取請求中的key 8             string key = Request["key"]; 9             if (Session[key] != null)10             {11                 // 這裡還需要操作資料庫12                 Msg = "啟用成功,Userid為" + Session[key];13             }14 15         }16     }
View Code

 

.NET下用C#實現郵箱啟用功能

聯繫我們

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