Achieve the effect: the customer enters the email address, sends a verification code to the customer mailbox (valid time is 20 minutes), the customer according to the mailbox the verification code, enters the verification, and carries on the judgment
Realize:
Page code:
<%@ Page language="C #"autoeventwireup="true"codefile="Default.aspx.cs"inherits="_default"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"><meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Form1"runat="Server"> <div>Please enter the receiving address:<asp:textbox id="TextBox1"runat="Server"></asp:TextBox> <%--prompts for the correctness of the input mailbox format--%> <asp:label id="Label1"runat="Server"text="Label"></asp:label><br/> <asp:button id="Button1"runat="Server"text="Get Verification Code"/><br/>Please enter the email verification code:<asp:textbox id="TextBox4"runat="Server"></asp:TextBox> <asp:button id="Button2"runat="Server"text="Judging"/><br/> <%--Verify correct reminders--%> <asp:label id="Label3"runat="Server"text="Label"></asp:label><br/> <asp:hiddenfield id="HiddenField1"runat="Server"/> </div> </form></body>"Text/javascript">//Regular expressions to determine the format of the mailboxdocument.getElementById ("TextBox1"). onkeyup =function () {vars = 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 ="Mailbox is correct"; } Else{document.getElementById ("Label1"). InnerText ="Mailbox Error"; } } </script>
Background code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Net;usingSystem.Net.Mail; Public Partial class_default:system.web.ui.page{stringYzm =NULL; protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; Button2.click+=button2_click; } //Compare user input to content stored in session voidButton2_Click (Objectsender, EventArgs e) { stringAA =Textbox4.text; if(AA = = session["Yzm"]. ToString ()) {Label3.text ="Validation Successful"; } Else{Label3.text ="validation Failed"; } } voidButton1_Click (Objectsender, EventArgs e) { //E -mail address, title, content stringto =TextBox1.Text; //Create a Q server object that sends a message reference the namespace firstSmtpClient SMTP =NewSmtpClient ("smtp.qq.com"); Smtp. Enablessl=true; Smtp. useDefaultCredentials=false; //Sender's addressMailAddress from=NewMailAddress ("[email protected]"); //Recipient's addressMailAddress T =Newmailaddress (to); //send the message from where to send to whoMailMessage mail =NewMailMessage ( from, T); //Assigning a value to a message headerMail. Subject ="Welcome to register XX Platform"; //assigning values to message content//randomly generate a 4-digit verification code that allows the customer to enter and determine if the verification code is correctRandom r =NewRandom (); Yzm=NULL; for(inti =0; I <4; i++) {Yzm+=r.next (0, -); } //Save the generated verification code in sessionsession["Yzm"] =Yzm; Mail. Body=Yzm; //Create a Certificate objectNetworkCredential net =NewNetworkCredential ("2580497963","Dzdeexcfposfecea"); //set up a qualification certificateSmtp. Credentials =net; //Send mailSMTP. Send (mail); }}
2017-6-10 ASP. NET Mailbox authentication and judgment