邮箱激活,注册信息

----------region.aspx前台-------------
<body>
    <form id="form1" runat="server">
    <div>
   
        <table style="width:50%;">
            <tr>
                <td>
                    用户名:</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    用户密码:</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    邮箱地址:</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
              <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="注册" οnclick="Button1_Click" /></td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>

--------region.aspx.cs后台-----------------

  public void sendMail(string email, string activeCode,int id)
        {

            MailMessage msg = new MailMessage();

            msg.From = new MailAddress("邮箱详细账号");
            msg.To.Add(email);
            msg.Subject = "请激活注册";

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


            msg.Body = contentBuilder.ToString(); ;

            msg.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();
            client.Host = "smtp.126.com";
            client.Port = 25;

            NetworkCredential credetial = new NetworkCredential();
            credetial.UserName = "@之前的部分";
            credetial.Password = "邮箱密码";

            client.Credentials = credetial;

            client.Send(msg);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string userName = this.TextBox1.Text;
            string password = this.TextBox2.Text;
            string email = this.TextBox3.Text;
            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) select @@identity";
                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 = Convert.ToInt32(cmd.ExecuteScalar());
                }
            }
            if (number > 0)
            {
                sendMail(email, activeCode,number);
            
                Response.Redirect("regionMessage.aspx");
            }
            else
            {
                Response.Write("注册失败,请重新注册!");
            }
        }

---------CheckActiveCode.aspx.cs后台-------------------------------

 protected void Page_Load(object sender, EventArgs e)
        {
          
            int id = Convert.ToInt32(Request["id"]);
            string activeCode = Request["activecode"].ToString();

           
            string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            int number;
            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)
            {
             
                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("用户不存在,还没有注册成功!");
            }
        }

 

--------Web.config-----------------
  <connectionStrings>
    <add name="conStr" connectionString="data source=.;initial catalog=student;user id=sa;password=111111"/>
  </connectionStrings>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值