自制邮箱激活功能全套

写了一个邮箱激活的功能使用的SMTP简单协议希望大神给点好的建议优化一下,代码如下

string ConSql = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
            string Url = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, "") + Request.ApplicationPath;
            using (SqlConnection conn = new SqlConnection(ConSql))
            {
                conn.Open();
                string sql = "INSERT INTO [Info] ([Title],[Details],[Email]) VALUES(@titles,@details,@email) SELECT @@IDENTITY";
                SqlParameter[] para ={
                                         new SqlParameter("@titles",this.TxtName.Text),
                                         new SqlParameter("@details",this.TxtDetails.Text),
                                         new SqlParameter("@email",this.TxtEmail.Text)
                                    };
                SqlCommand comm = new SqlCommand(sql, conn);


                string sql2 = "SELECT ID FROM [Info] WHERE ID=@@IDENTITY";
                SqlCommand comm1 = new SqlCommand(sql2, conn);
                comm.Parameters.AddRange(para);
                if (comm.ExecuteNonQuery() > 0)
                {
                    string to = this.TxtEmail.Text.Trim();
                    string title = "来自星星的你发来的邮件";
                    string content = "恭喜你完成了注册,<a href=" + Url + "CheckInfo.aspx?id=" + comm1.ExecuteScalar().ToString() + ">点击链接激活帐号</a>";

                    string Host = "smtp.qq.com";  //STMP服务器地址
                    string Account = "864684766@qq.com";  //SMTP服务帐号
                    string Pwd = "***";   //SMTP服务密码
                    string From = "864684766@qq.com";  //发送方邮件地址

                    bool s = sendmail(to, title, content, Host, Account, Pwd, From);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('注册成功,我们已经在您的邮箱中发了邮件,请激活账号后登陆');</script>");
                }
            }

 

 这里以使用QQ邮箱为例,根据前台输入的注册邮箱地址截取字符串判断是什么样的邮箱(163 、sina)等然后使用smtp.qq.com服务器进行发送,当然如果你使用的是新浪邮箱,你的服务器就要改成smtp.sina.com以此类推,163以及126同理,但是我始终觉得这里有些代码的冗余,关于URL,也是获得整个的URL之后,再去掉非根目录,之后加上“\”符号,可以获得根目录,这个就是点击连接之后获得的激活地址。

            string ConStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();

            //Response.Write(Request.QueryString["id"]);
            using (SqlConnection conn = new SqlConnection(ConStr))
            {
                conn.Open();
                string sql = "UPDATE [Info] SET [Statue]=@statue WHERE [Id]=" + Request.QueryString["id"];
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlParameter para = new SqlParameter("@statue", 1);
                comm.Parameters.Add(para);
                if (comm.ExecuteNonQuery() < 0)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('激活失败!');</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('激活成功!');</script>");
                }
            }

这是激活页面的代码。日后根据statue字段判断是否是激活状态0未激活,1激活。

以上就是我的一些想法了,和大家共同分享希望大家指教

 

 

补上一个发送邮件的方法SMTP方式:

 /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="fromAddressNo">发件人邮箱号</param>
        /// <param name="fromAddressName">发件人邮箱姓名</param>
        /// <param name="toAddressNo">收件人邮箱号</param>
        /// <param name="toAddressName">收件人邮箱姓名</param>
        /// <param name="sendTitle">发送内容标题</param>
        /// <param name="sendContent">发送内容正文</param>
        /// <param name="smtpHost">邮件服务器</param>
        /// <param name="userName">发送人邮箱账号</param>
        /// <param name="Pwd">发送人邮箱密码/授权码</param>
        /// <param name="smtpPort">smtp主机上的端口号,默认是25</param>
        public void SendEmailMessage(string fromAddressNo,string fromAddressName, string toAddressNo, string toAddressName, string sendTitle,string sendContent, string smtpHost,string userName,string Pwd,int smtpPort=25)
        {
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Host = smtpHost;//邮件服务器
            client.Port = smtpPort;//smtp主机上的端口号,默认是25.
            client.DeliveryMethod = SmtpDeliveryMethod.Network;//邮件发送方式:通过网络发送到SMTP服务器
            client.Credentials = new NetworkCredential(userName, Pwd);//凭证,发件人登录邮箱的用户名和密码,QQ新改版后设置SMTP时会生成授权码替代密码
            //电子邮件信息类
            MailAddress fromAddress = new MailAddress(fromAddressNo, fromAddressName);//发件人Email,在邮箱是这样显示的,[发件人:小明<panthervic@163.com>;]
            MailAddress toAddress = new MailAddress(toAddressNo, toAddressName);//收件人Email,在邮箱是这样显示的, [收件人:小红<43327681@163.com>;]
            MailMessage mailMessage = new MailMessage(fromAddress, toAddress);//创建一个电子邮件类
            mailMessage.Subject = sendTitle;
            mailMessage.IsBodyHtml = true;//如果要发送HTML这里要设置为true
            mailMessage.Body = sendContent;//可为html格式文本
            //mailMessage.Body = "邮件的内容";//可为html格式文本
            mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件主题编码
            mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");//邮件内容编码
            mailMessage.IsBodyHtml = true;//邮件内容是否为html格式
            mailMessage.Priority = System.Net.Mail.MailPriority.High;//邮件的优先级,有三个值:高(在邮件主题前有一个红色感叹号,表示紧急),低(在邮件主题前有一个蓝色向下箭头,表示缓慢),正常(无显示).
            try
            {
                client.Send(mailMessage);//发送邮件
                //client.SendAsync(mailMessage, "ojb");异步方法发送邮件,不会阻塞线程.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

 

posted @ 2014-08-06 11:55  洛晨随风  阅读(431)  评论(0编辑  收藏  举报