首页 > 代码库 > 自制邮箱激活功能全套

自制邮箱激活功能全套

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

public static bool sendmail(string to, string title, string content, string Host, string Account, string Pwd,string From)        {            SmtpClient _smtpClient = new SmtpClient();            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式            _smtpClient.Host = Host;//指定SMTP服务器            _smtpClient.Credentials = new System.Net.NetworkCredential(Account, Pwd);//用户名和密码            _smtpClient.EnableSsl = true;            MailMessage _mailMessage = new MailMessage(From, to);            _mailMessage.Subject = title;//主题            _mailMessage.Body = content;//内容            _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码            _mailMessage.IsBodyHtml = true;//设置为HTML格式            _mailMessage.Priority = MailPriority.High;//优先级            try            {                _smtpClient.Send(_mailMessage);                return true;            }            catch            {                return false;            }        }

这一段是设置一下发送的内容以及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 email = this.TxtEmail.Text.Trim();                    string afterSubEmail=email.Substring(email.IndexOf("@")+1).Trim();                    if(afterSubEmail.Equals("163.com"))                    {                        string to = this.TxtEmail.Text.Trim();                        string title = "来自星星的你发来的邮件";                        string content = "恭喜你完成了注册,<a href=http://www.mamicode.com/" + 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>");                    }                    else if (afterSubEmail.Equals("126.com"))                    {                        string to = this.TxtEmail.Text.Trim();                        string title = "来自星星的你发来的邮件";                        string content = "恭喜你完成了注册,<a href=http://www.mamicode.com/" + 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>");                    }                    else if (afterSubEmail.Equals("sina.com"))                    {                        string to = this.TxtEmail.Text.Trim();                        string title = "来自星星的你发来的邮件";                        string content = "恭喜你完成了注册,<a href=http://www.mamicode.com/" + 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>");                    }                    else if (afterSubEmail.Equals("qq.com"))                    {                        string to = this.TxtEmail.Text.Trim();                        string title = "来自星星的你发来的邮件";                        string content = "恭喜你完成了注册,<a href=http://www.mamicode.com/" + 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激活。

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