邮件发送并激活

it2022-05-20  71

第一步:首先新建一个页面register页面,在该页面的前台添加上用户名、密码、邮箱地址和三个文本框以及注册按钮。

第二步:在register页面的register.aspx.cs页面加上以下代码:

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

            MailMessage msg = new MailMessage();                       msg.From = new MailAddress("q******09@126.com");填写一个126的邮箱            msg.To.Add(email);            msg.Subject = "请激活注册";

            StringBuilder contentBuilder = new StringBuilder();            contentBuilder.Append("请单击以下连接完成激活!");            contentBuilder.Append("<a href='http://localhost:8899/CheckActiveCode.aspx?activecode="+activeCode+"&id="+number+"'>激活</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 = "qzc900809";            credetial.Password = "900809";

            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;//在web.Confing中设置连接数据库字符串            int number;            using (SqlConnection con = new SqlConnection(conStr))            {                string sql = "insert into email (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);                //sendMail(string email,string activeCode,int id);//给注册用户发邮件

                Response.Redirect("regionMessage.aspx");///这个可有可无,只是一个提示成功的页面            }            else            {                Response.Write("注册失败,请重新注册!");            }        }

第四步:再重新新建一个CheckActiveCode.aspx页面,然后再该页面(CheckActiveCode.aspx.cs)的Page_Load中

加上以下代码:

    //1取出参数id            int  id = Convert.ToInt32(Request["zcid"]);            string activeCode = Request["activecode"].ToString();

            //2判断id为id的记录是否存在。            //连接数据库            string conStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;             int number;            using (SqlConnection con = new SqlConnection(conStr))            {                string sql = "select count(*) from Registor where zcid=@id";                using (SqlCommand cmd=new SqlCommand(sql,con))                {                    con.Open();                    cmd.Parameters.AddWithValue("@id",id);                  number=Convert.ToInt32(cmd.ExecuteScalar());                }                            }            if (number > 0)            {                //如果该用户存在取出其ActiveCode字段进行比较。如果一样。把Ative字段修改为true

                //连接数据库

                string AC;                using (SqlConnection con = new SqlConnection(conStr))                {                    string sql = "select ActiveCode from Registor where zcid=@id";                    using (SqlCommand cmd = new SqlCommand(sql, con))                    {                        con.Open();                        cmd.Parameters.AddWithValue("@id", id);                        AC=cmd.ExecuteScalar().ToString();//wrong?

                    }

                }                if (activeCode == AC)                {                    Response.Write("激活成功!");                    using (SqlConnection con = new SqlConnection(conStr))                    {                        string sql = "update Registor set Active=1 where zcid=@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("用户不存在,还没有注册成功!");            }        } 第五步:这样完成之后,就可以去自己刚才填写的126邮箱中查看邮件,打开邮箱单击激活,就激活成功了,

然后再去数据库中查看Active的状态,此时应该是True,这样,一个完整的注册并发送邮件加激活的功能就完成了。

 查看更多文章:http://write.blog.csdn.net/postedit/8483917

转载于:https://www.cnblogs.com/qzc900809/archive/2013/01/09/2853759.html


最新回复(0)