javamail实现注册激活邮件
本文实例为大家分享了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邮箱中有一封注册邮箱,点击该邮箱中的链接,就进入了激活页面,然后该注册的账号就激活注册成功了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)