2017-6-10 ASP.NET邮箱认证并进行判断

 

实现效果:客户输入邮箱地址,向客户邮箱中发送一条验证码(有效时间为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">
    //正则表达式,对邮箱格式进行判断
    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("2580497963@qq.com");
        //收件人的地址
        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);

    }
}

 

posted @ 2017-06-10 21:44  青年a  阅读(1101)  评论(0编辑  收藏  举报