C#发送邮件验证账号


账号注册时往往会需要邮箱验证,那么验证实现流程是什么呢?

第一步:用户表中包括用户账号,密码,邮箱,验证随机码,注册时间等字段。


第二步:注册页面,让用户录入用户信息


第三步:注册方法中,先把新增用户信息插入到数据库中,然后给刚注册的邮箱发邮件,邮件内容包括一个激活链接(包括用户账号和随机验证码)

        string validateCode = Guid.NewGuid().ToString("N");
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
        int i = 0;
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            DateTime dateTime = DateTime.Now;
            cmd.CommandText = "insert into User_Info(Account,Password,Email,ValidateCode,RegisterTime) values ('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox1.Text + "','" + validateCode + "','"+dateTime+"')";
            i = cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception)
        {

            throw;
            return;
        }
    if (i>0)
        {
            try
            {
                var appSettings = System.Configuration.ConfigurationManager.AppSettings;//获取config配置信息               
                string senderServerIp = appSettings["senderServerIp"];//发送地址服务器
                string toMailAddress = TextBox1.Text;//接受方地址
                string fromMailAddress = appSettings["fromMailAddress"];//发送方地址
                string mailUsername = appSettings["mailUsername"]; //发送账户
                string mailPassword = appSettings["mailPassword"]; //发送邮箱的密码
                string mailPort = appSettings["mailPort"];//发送服务器端口号
                System.Text.StringBuilder strBody = new System.Text.StringBuilder();//邮件内容
                strBody.Append("点击下面链接激活账号,48小时生效,否则重新注册账号,链接只能使用一次,请尽快激活!");
                strBody.Append("点击这里");
                strBody.Append("如未能激活请点击下面链接:http://localhost:38216/Validate.aspx?Account=" + TextBox2.Text + "&ValidateCode=" + Decryption.Encrypt(validateCode) + "");
                Email email = new Email(senderServerIp, toMailAddress, fromMailAddress, "邮箱验证", strBody.ToString(), mailUsername, mailPassword, mailPort, true, true);
                email.Send();
            }
            catch (Exception ex)
            {
            }
        }
这里把随机验证码进行了加密


第四步:用户点击激活链接时,要在激活页面进行验证。利用用户账号和随机验证码进行查找,另外可以把当前时间与用户注册时间比较,超过有效时间也不能激活

        string account = Request["Account"];
        string code = Decryption.Decrypt(Request["ValidateCode"]);

        string sql = "select * from User_Info where Account='" + account + "' and ValidateCode='" + code + "'";
        DataTable dt = Sqlhelper.Query(sql, null).Tables[0];

        
        if (dt.Rows.Count>0)
        {
            TimeSpan ts = DateTime.Now - Convert.ToDateTime(dt.Rows[0]["RegisterTime"]);
            int i = 0;
            if (ts.TotalHours<48)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                string commandtext = "update User_Info set State=1 where usercode=" + Convert.ToInt32(dt.Rows[0]["usercode"]);
                i=Sqlhelper.ExecuteNonQuery(CommandType.Text, commandtext, null);
                if (i>0)
                {
                    TextBox1.Text = "验证成功";
                }
                
            }
            else
            {
                TextBox1.Text = "此账号激活时长已超过48小时。请重新注册新账号";
            }
            
        }
        else
        {
            TextBox1.Text = "验证失败";
        }



这里把随机验证码相应的进行了解密,验证成功后,可以跳转到主页面或者登陆页面!


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值