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

来源:互联网 发布:异或校验算法 编辑:程序博客网 时间:2024/04/20 18:28

**********************Mail.aspx.cs

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 webMail
{
    public partial class Mail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        int number = 0;
        public void sendMail(string email, string activecode)//发邮件的方法
        {
            System.Net.Mail.MailMessage msg = new MailMessage();//创建电子邮件
            msg.From = new MailAddress("liuxiaofei0809@126.com");//邮件的由来
            msg.To.Add(email);//收件人的地址
            msg.Subject = "请激活注册";
            StringBuilder connentBuilder = new StringBuilder();
            connentBuilder.Append("请单击一下链接完成激活");
            connentBuilder.Append("<a href=http://localhost:50988/CheckActiveCode.aspx?activecode=" + activecode + "&id=" +number+ ">" + "请单击连接完成激活" + "</a>");

            msg.Body = connentBuilder.ToString();
            msg.IsBodyHtml = true;

            SmtpClient sc = new SmtpClient();//传输smtp
            sc.Host = "smtp.126.com";
            sc.Port = 25;//发件方端口

            NetworkCredential credential = new NetworkCredential();
            credential.UserName = "liuxiaofei0809@126.com";
            credential.Password = "xiaofei0809";
            sc.Credentials = credential;
            sc.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;
            using(SqlConnection con=new SqlConnection (constr))
            {
                string sql = "insert into Mail (UserName,Password,Email,Active,Activecode) output inserted.id values(@username,@password,@email,@active,@activecode)";
                SqlParameter[] prm = 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(prm);
                    number=(int) cmd.ExecuteScalar();
                }
            }
            if (number > 0)
            {
                sendMail(email, activecode);
                Response.Redirect("regionMessage.aspx");

            }
            else
            {
                Response.Write("注册失败,请重新注册");
           
            }
        }
      
       
    }
}

***********************regionMessage.aspx

<div>
    <h3>恭喜您注册成功</h3>
    </div>

*****************CheckActiveCode.aspx.cs

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;

namespace webMail
{
    public partial class CheckActiveCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int number;
            //取出参数
             int id= Convert.ToInt32(Request["id"]);
             string activeCode = Request["activecode"].ToString();
            //链接数据库
             string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string sql = "select count(*) from Mail where id=@id";
                using(SqlCommand cmd=con.CreateCommand())
                {
                    con.Open();
                    cmd.CommandText = sql;
                    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 Mail where id=@id";
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        con.Open();
                        cmd.CommandText = sql;
                        cmd.Parameters.AddWithValue("@id", id);
                        AC = cmd.ExecuteScalar().ToString();
                    }
                }
                if (activeCode == AC)
                {
                    Response.Write("激活成功");
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        string sql = "update Mail set Active=1 where id=@id";
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            con.Open();
                            cmd.CommandText = sql;
                            cmd.Parameters.AddWithValue("@id", id);
                            number = Convert.ToInt32(cmd.ExecuteScalar());
                        }
                    }
                }
                else
                {
                    Response.Write("用户已存在,但没有注册成功");
                }
            }
            else
            {
                Response.Write("用户不存在,还没有注册成功");
           
            }
        }
    }
}

原创粉丝点击