javamail实现注册激活邮件
程序员文章站
2024-02-29 12:48:52
本文实例为大家分享了javamail注册激活邮件的具体代码,供大家参考,具体内容如下
需要的jar包
1.mysql驱动包
2.mail.jar包
smtp和...
本文实例为大家分享了javamail注册激活邮件的具体代码,供大家参考,具体内容如下
需要的jar包
1.mysql驱动包
2.mail.jar包
smtp和pop3协议概述
smtp协议称为简单邮件传输协议,是一组用于从原地址到目的地址传送邮件的规则,由它来控制信件的中转方式。smtp协议属于tcp/ip的协议簇,smtp是负责邮件服务器之间的寄信的通信协定 pop3协议称为邮局协议版本3,也是tcp/ip协议簇的一员,基于pop3协议的服务器是用来接收信件的。每个email地址一般只有一个如果想要同时收取多个邮箱的信件,就需要挨个设置每个邮箱的pop3服务器地址。
核心代码实现
#创建数据库表 create database javamail; use javamail; create table user( id int primary key auto_increment, username varchar(30) not null, password varchar(30) not null, nickname varchar(30) not null, email varchar(30) not null, state int default 0, #标识是否已经激活 0未激活1已激活 code varchar(64) not null ); //创建数据库对应的javabean public class userbean{ private integer id; private string username; private string password; private string nickname; private string email; private integer state; private string code; //省略setter and getter } //数据库操作类userdao public class userdao{ private connection conn ; //获取数据库连接 public void getconn(){ class.forname("com.mysql.jdbc.driver"); string url = "mysql:jdbc://localhost:3306/javamail"; string rootname = "root"; string rootpass = "chengxi"; conn = drivermanager.getconnection(url,rootname,rootpass); } //注册 public void regist(userbean user){ string sql = "insert into user(username,password,nickname,email,code) values(?,?,?,?,?)"; preparedstatement pstmt = conn.preparedcall(sql); pstmt.setstring(1,user.getusername()); pstmt.setstring(2,user.getpassword()); pstmt.setstring(3,user.getnickname()); pstmt.setstirng(4,user.getemail()); pstmt.setstring(5,user.getcode()); pstmt.executeupdate(); } //激活 public void activebycode(string code){ string sql = "update user set state=1,code=null where code=?"; preparedstatement pstmt = conn.preparedcall(sql); pstmt.setstring(1,code); pstmt.executeupdate(); } //关闭数据库连接 public void closeconn(){ if(conn != null){ conn.close(); } } } //一个工具类,用于获取随机code注册激活码 public class uuidutils{ public static string getuuid(){ return uuid.randomuuid().tostring().replace("-","")+uuid.randomuuid().tostring().replace("-",""); } } //index.jsp前端页面实现 <form action="<%=request.getcontextpath()%>/regist.do" method="post"> <tr> <td><label for="username">账号</label></td> <td><input type="text" name="username" /></td> </tr> <tr> <td><label for="password">密码</label></td> <td><input type="text" name="password" /></td> </tr> <tr> <td><label for="nickname">昵称</label></td> <td><input type="text" name="nickname" /></td> </tr> <tr> <td><label for="email">邮箱</label></td> <td><input type="text" name="email" /></td> </tr> <tr> <td><input type="submit" value="注册" /></td> <td><input type="reset" name="重写" /></td> </tr> </form> //发送邮件的utils工具类 public class sendmailuitls{ public static void sendmail(string to,string code){ //session是mail包下的,因为是本地测试,所以第一个参数直接创建默认的即可 session session = session.getinstance(new properties(),new authenticator() { @override protected passwordauthentication getpasswordauthentication(){ return new passwordauthentication("server@store.com", "111"); } }); //创建邮件对象 message message = new mimemessage(session); //设置发件人,这里我创建了一个server@store.com用于发送邮件 message.setfrom(new internetaddress("server@store.com")); message.setrecipient(recipienttype.to, new internetaddress(to)); message.setsubject("来自个博网的注册激活邮件"); message.setcontent("<a href='http://localhost:8080/registmailweb/active.do'>点我激活</a>","text/html;charset=utf-8"); //发送邮件 transport.send(message); } } //regist.doservlet实现 public class registservlet extends httpservlet{ public void init(){ super.init(); } public void service(httpservletrequest request,httpservletresponse response){ //获取数据 string username = request.getparameter("username"); string password = request.getparameter("password"); string nickname = request.getparameter("nickname"); string email = request.getparameter("email"); //封装数据 userbean user = new userbean(); user.setusername(username); user.setpassword(password); user.setnickname(nickname); user.setemail(email); user.setcode(uuidutils.getuuid()); //注册 userdao dao = new userdao(); dao.getconn(); dao.regist(user); dao.closeconn(); //发送邮件 sendmailutils.sendmail(email,user.getcode()); } public void destroy(){ super.desroy(); } } //创建active.do servlet用于激活邮箱 public class activeservlet httpservlet{ public void init(){ super.init(); } public void service(httpservletrequest request,httpservletresponse response){ //获取数据 string code = request.getparameter("code"); //数据库判断对应code是否存在,(这里就不进行判断了,直接进行比对)state=1&code=null代表激活 userdao dao = new userdao(); dao.activebycode(code); //重定向success.jsp request.getrequestdispatcher("/web-inf/success.jsp").forward(request,response); } public void destroy(){ super.destroy(); } }
编写成功页面
<html> <head> <title>成功激活</title> </head> <body> <script> window.onload = function(){ alert("已成功激活"); }; </script> </body> </html>
web.xml配置两个servlet
<servlet> <servlet-name>registservlet</servlet-name> <servlet-class>com.servlet.registservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>registservlet</servlet-name> <url-pattern>/regist.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>activeservlet</servlet-name> <servlet-class>com.servlet.activeservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>activeservlet</servlet-name> <url-pattern>/active.do</url-pattern>
效果
打开tomcat服务器和foxmail邮件服务器,然后进入该web首页,点击注册,可以在foxmail中看到对应的email@store.com邮箱中有一封注册邮箱,点击该邮箱中的链接,就进入了激活页面,然后该注册的账号就激活注册成功了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 理解Java当中的回调机制(翻译)