標籤:new var partial 技術 text 地址 event 技術分享 oev
實現效果:客戶輸入郵箱地址,向客戶郵箱中發送一條驗證碼(有效時間為20分鐘),客戶根據郵箱中的驗證碼,輸入驗證,並進行判斷
實現:
頁面代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title></head><body> <form id="form1" runat="server"> <div> 請輸入接收地址:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <%-- 對輸入郵箱格式的正確與否進行提示 --%> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /> <asp:Button ID="Button1" runat="server" Text="擷取驗證碼" /><br /> 請輸入郵箱驗證碼:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <asp:Button ID="Button2" runat="server" Text="判斷" /><br /> <%-- 驗證正確進行提醒 --%> <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br /> <asp:HiddenField ID="HiddenField1" runat="server" /> </div> </form></body></html><script type="text/javascript"> //Regex,對郵箱格式進行判斷 document.getElementById("TextBox1").onkeyup = function () { var s = document.getElementById("TextBox1").value; if (/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(s)) { document.getElementById("Label1").innerText = "郵箱正確"; } else { document.getElementById("Label1").innerText = "郵箱錯誤"; } } </script>
後台代碼:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Net;using System.Net.Mail;public partial class _Default : System.Web.UI.Page{ string yzm = null; protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; Button2.Click += Button2_Click; } //根據使用者輸入的內容和session中存放的內容進行比較 void Button2_Click(object sender, EventArgs e) { string aa = TextBox4.Text; if (aa == Session["yzm"].ToString()) { Label3.Text = "驗證成功"; } else { Label3.Text = "驗證失敗"; } } void Button1_Click(object sender, EventArgs e) { //發送郵件的地址,標題,內容 string to = TextBox1.Text; //建立發送郵件的q伺服器對象先引用命名空間 SmtpClient smtp = new SmtpClient("smtp.qq.com"); smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; //寄件者的地址 MailAddress from = new MailAddress("[email protected]"); //收件者的地址 MailAddress t = new MailAddress(to); //發送的郵件來自哪裡,發送給誰 MailMessage mail = new MailMessage(from, t); //給郵件標題賦值 mail.Subject = "歡迎註冊xx平台"; //給郵件內容賦值 //隨機產生一個4位元的驗證碼,讓客戶輸入,判斷驗證碼是否正確 Random r = new Random(); yzm = null; for (int i = 0; i < 4;i++ ) { yzm+=r.Next(0,100); } //將產生的驗證碼存入session中 Session["yzm"] = yzm; mail.Body = yzm; //建立認證對象 NetworkCredential net = new NetworkCredential("2580497963", "dzdeexcfposfecea"); //設定資格認證 smtp.Credentials = net; //發送郵件 smtp.Send(mail); }}
2017-6-10 ASP.NET郵箱認證並進行判斷