Lately our MSP group has been coming up with all sorts of wild ideas, like making those bro-purple dog tags (⁄ ⁄•⁄ω⁄•⁄ ⁄), and we're also planning to build an MSP matchmaking website. While working on it, I started thinking about how to use ASP.NET to send a verification code to an email address to check that the address actually works.
Here's my approach:
First, use the RegularExpressionValidator control to check that the email format is valid
Then use the Random class to get a pseudo-random number
Send the random number to the user's mailbox using the MailMessage and SmtpClient classes
The user enters the random number to confirm the email address works
Fire up the wholesome Visual Studio 2017 [many thanks to Microsoft daddy for all the license keys!]. If you don't have the Professional/Enterprise edition, you can use the Community edition of VS! _:(´ཀ`」 ∠):_
Front-end code for entering the email address to receive the message:
输入邮箱:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="发送验证码" OnClick="Button1_Click" />
Back-end code for entering the email address to receive the message:
string chkCode = string.Empty;
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
MailMessage mymail = new MailMessage(); //声明一个Mail对象
mymail.From = new MailAddress("xxxxx@163.com"); //发件人地址
mymail.To.Add(new MailAddress(TextBox1.Text)); //收件人地址
mymail.Subject = "XXX网站验证码"; //邮件主题
mymail.Body = chkCode.ToString(); //发送邮件的内容
//创建一个邮件服务器类
SmtpClient myclient = new SmtpClient();
myclient.Host = "smtp.163.com";
//SMTP服务端口
myclient.Port = int.Parse("25");
//验证登录
myclient.Credentials = new NetworkCredential("xxxxx@163.com", "xxxxx");//这里填入发件人的用户名和密码
myclient.Send(mymail);
Session["chkCode"] = chkCode; //将验证码传值到Default2
Response.Redirect("Default2.aspx"); //跳转至Default2页面
Front-end code for the user to enter the verification code and check the email address:
输入验证码:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="RegularExpressionValidator" ValidationExpression="^[0-9]{4}$"></asp:RegularExpressionValidator>
<br />
<asp:Button ID="Button2" runat="server" Text="检查邮箱" OnClick="Button2_Click" />
Back-end code for the user to enter the verification code and check the email address:
String chkCode; //定义chkCode变量
chkCode = Session["chkCode"].ToString(); //接收Default传值
if (TextBox2.Text == chkCode) //判断验证码是否正确
{
Response.Write("<script languane='javascript'>alert('验证码正确!')</script>");
}
else
{
Response.Write("<script languane='javascript'>alert('验证码错误!')</script>");
}
That's my approach and code for verifying email addresses during registration in ASP.NET!