邮箱验证码

本文介绍了一个使用ASP.NET实现的简单邮箱验证码功能示例,包括前端页面布局和后端逻辑处理。通过随机生成验证码并将其发送到指定邮箱,用户在输入框中输入收到的验证码进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%@ Page Language="C#" Debug="true" 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>&nbsp;箱:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="发送验证码" /><br /><br />
        验证码:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="" ForeColor="Red"></asp:Label><br /><br />
        <asp:Button ID="Button2" runat="server" Text="验证" />
    </div>
    </form>
</body>
</html>
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
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
        Button2.Click += Button2_Click;
    }

    //发送按钮
    void Button1_Click(object sender, EventArgs e)
    {
        //创建外部的smtp服务客户端对象
        SmtpClient smtp = new SmtpClient("smtp.sina.cn");

        //创建证书对象
        NetworkCredential cred = new NetworkCredential("18560812711@sina.cn", "hq1234561");

        //服务器证书指向
        smtp.Credentials = cred;

        //验证码
        Random r = new Random();
        string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        string yzm = str.Substring(r.Next(0 ,str.Length ),4);
        Session["yzm"] = yzm;

        //标题
        string subject = "来自XX官网的邮件验证";

        //内容
        string context = "您的邮箱验证码为[" + yzm + "],请在20分钟内填写验证!此邮件为系统邮件,勿回复!";

        //将邮件发送到指定服务器
        smtp.Send("18560812711@sina.cn", TextBox1.Text.Trim(), subject, context);
    }

    //验证按钮
    void Button2_Click(object sender, EventArgs e)
    {
        if (TextBox2.Text.ToUpper() == Session["yzm"].ToString().ToUpper())
        {
            Label1.Text = "验证码错误";
        }
        else { Label1.Text = ""; }
    }

}

 

转载于:https://www.cnblogs.com/xiao55/p/6012080.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值