本文实例为大家分享了javamail注册激活邮件的具体代码,供大家参考,具体内容如下
需要的jar包
1.MySQL驱动包
2.mail.jar包
SMTP和POP3协议概述
SMTP协议称为简单邮件传输协议,是一组用于从原地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP协议属于TCP/IP的协议簇,SMTP是负责邮件服务器之间的寄信的通信协定 POP3协议称为邮局协议版本3,也是TCP/IP协议簇的一员,基于POP3协议的服务器是用来接收信件的。每个Email地址一般只有一个如果想要同时收取多个邮箱的信件,就需要挨个设置每个邮箱的POP3服务器地址。
核心代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#创建数据库表 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(); } } |
编写成功页面
1
2
3
4
5
6
7
8
9
10
11
12
|
< html > < head > < title >成功激活</ title > </ head > < body > < script > window.onload = function(){ alert("已成功激活"); }; </ script > </ body > </ html > |
web.xml配置两个Servlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< 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邮箱中有一封注册邮箱,点击该邮箱中的链接,就进入了激活页面,然后该注册的账号就激活注册成功了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。