• 简单的实现用户注册时,向其油箱发送激活码邮件,并进行状态处理。 .


    注册页面:

    后台:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Net.Mail;
    using System.Text;
    using System.Net;

    namespace 激活验证
    {
        public partial class region : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }

             public void sendMail(string Email,string activeCode)
            {
                MailMessage msg = new MailMessage();
                msg.From=new MailAddress("duan_linlin@163.com");  //邮件来自哪
                msg.To.Add(Email);
                 msg.Subject = "请激活注册!";


                 StringBuilder contentBuilder=new StringBuilder();
                 contentBuilder.Append("请单击一下连接完成激活!");
                 contentBuilder.Append("<a href='http://localhost:1950/CheckActiveCode.aspx?activecode="+activeCode+"&id=3'>激活</a>");

                 msg.Body = contentBuilder.ToString();

                 msg.IsBodyHtml=true;
                 SmtpClient client = new SmtpClient(); //允许传输协议
                 client.Host = "smtp.163.com";   //发件方服务器地址
                 client.Port = 25;  //发件方端口
                 NetworkCredential credential = new NetworkCredential();
                 credential.UserName = "duan_linlin@163.com";
                 credential.Password = "jinyuxueqi521";
                 client.Credentials = credential;  //说明证书要给代理证书credential
                 client.Send(msg);
            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                string userName = TextBox1.Text.Trim();
                string password = TextBox2.Text.Trim();
                string Email = TextBox3.Text.Trim();

                string activeCode = Guid.NewGuid().ToString().Substring(0, 8);  //生成激活码

                string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
              
                int number;
                using (SqlConnection con=new SqlConnection(conStr))
                {
                    string sql = "insert into T_Users (UserName,Password,Email,Active,ActiveCode) values(@username,@password,@Email,@active,@activecode)";
                    SqlParameter[] prams = new SqlParameter[]{
                    new SqlParameter("@username",userName),
                    new SqlParameter("@password",password),
                    new SqlParameter("@Email",Email),
                    new SqlParameter("@active",false),
                    new SqlParameter("@activecode",activeCode)
                    };

                    using (SqlCommand cmd=new SqlCommand(sql,con))
                    {
                        con.Open();
                        cmd.Parameters.AddRange(prams);

                       number= cmd.ExecuteNonQuery();
                    }
                }

                if (number>0)
             {   
              sendMail( Email, activeCode);//给注册用户发邮件

                    Response.Redirect("regionMessage.aspx");
             }
                else
                 {
                   Response.Write("注册失败");
                   }
               
            }


          

        }
    }

    CheckActiveCode后台:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace 激活验证
    {
        public partial class CheckActiveCode : System.Web.UI.Page
        {    
            string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            int  number;
            protected void Page_Load(object sender, EventArgs e)
            {
                //去除参数id
                int id = Convert.ToInt32(Request["id"]);

                string activeCode = Request["ActiveCode"].ToString();

                //判断id为id的记录值是否存在
                //连接数据库
           
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    string sql = "select count(*) from T_Users where id=@id";
                    using (SqlCommand cmd=new SqlCommand(sql,con))
                    {
                        con.Open();
                        cmd.Parameters.AddWithValue("@id", id);

                        number = Convert.ToInt32(cmd.ExecuteScalar());

                    }
                }

                if ( number > 0)
                {
                    //如果该用户存在取出其ActiveCode字段进行比较,如果一样,把Active字段修改为true。
                    //连接数据库
                    string AC;
                    using (SqlConnection con = new SqlConnection(conStr))
                    {
                        string sql = "select ActiveCode from T_Users where id=@id";
                        using (SqlCommand cmd = new SqlCommand(sql, con))
                        {
                            con.Open();
                            cmd.Parameters.AddWithValue("@id", id);

                            AC =cmd.ExecuteScalar().ToString();

                        }
                    }


                    if (activeCode == AC)
                    {
                        Response.Write("激活成功!");

                        //连接数据库
                        using (SqlConnection con = new SqlConnection(conStr))
                        {
                            string sql = "update T_Users set Active=1 where id=@id";
                            using (SqlCommand cmd = new SqlCommand(sql, con))
                            {
                                con.Open();
                                cmd.Parameters.AddWithValue("@id", id);

                                number =Convert.ToInt32(cmd.ExecuteScalar());

                            }
                        }
                    }

                    else
                    {
                        Response.Write("用户存在,但是激活码错误!");
                    }

                }
                else
                {
                    Response.Write("用户不存在!注册失败!");           
                }
            }
        }
    }

    regionMessage前台:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="regionMessage.aspx.cs" Inherits="激活验证.regionMessage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
       <h2> 恭喜你注册成功!</h2>
        </div>
        </form>
    </body>
    </html>

  • 相关阅读:
    #pragma
    I/0概念介绍
    Android Eclipse 源码工程 调试
    SHELL四则运算和比较
    Android源码中添加 修改应用
    android ubuntu9.10 源码的编译 Eclipse工程 Contacts编译 应用加载
    【Wonder原创】LogMiner使用实践
    【转】Oracle入门教程,新手必读
    【Wonder原创】关于MSSQL通过DBLink访问Oracle问题
    【杂记】SQLServer
  • 原文地址:https://www.cnblogs.com/duanlinlin/p/2844309.html
Copyright © 2020-2023  润新知