现在很多的网站都提供有用户注册功能, 通常我们注册成功之后就会收到一封来自注册网站的邮件。邮件里面的内容可能包含了我们的注册的用户名和密码以及一个激活账户的超链接等信息。今天我们也来实现一个这样的功能,用户注册成功之后,就将用户的注册信息以Email的形式发送到用户的注册邮箱当中,实现发送邮件功能就得借助于JavaMail了。
一、搭建开发环境
1.1、创建Web项目
1.2、用户注册的Jsp页面
register.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML> < html > < head > < title >注册页面</ title > </ head > < body > < form action = "${pageContext.request.contextPath}/servlet/RegisterServlet" method = "post" > 用户名:< input type = "text" name = "username" >< br /> 密码:< input type = "password" name = "password" >< br /> 邮箱:< input type = "text" name = "email" >< br /> < input type = "submit" value = "注册" > </ form > </ body > </ html > |
1.3、消息提示页面
message.jsp
1
2
3
4
5
6
7
8
9
10
11
|
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML> < html > < head > < title >消息提示页面</ title > </ head > < body > ${message} </ body > </ html > |
二、编写处理用户注册处理程序
2.1、开发封装用户注册信息的domain
User.java
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
|
package me.gacl.domain; public class User { private String username; private String password; private String email; public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public String getEmail() { return email; } public void setEmail(String email) { this .email = email; } } |
2.2、编写邮件发送功能
发送邮件是一件非常耗时的事情,因此这里设计一个线程类来发送邮件
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
|
package me.gacl.web.controller; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import me.gacl.domain.User; /** * @ClassName: Sendmail * @Description: Sendmail类继承Thread,因此Sendmail就是一个线程类,这个线程类用于给指定的用户发送Email * @author: 孤傲苍狼 * @date: 2015-1-12 下午10:43:48 * */ public class Sendmail extends Thread { //用于给用户发送邮件的邮箱 private String from = "gacl@sohu.com" ; //邮箱的用户名 private String username = "gacl" ; //邮箱的密码 private String password = "邮箱密码" ; //发送邮件的服务器地址 private String host = "smtp.sohu.com" ; private User user; public Sendmail(User user){ this .user = user; } /* 重写run方法的实现,在run方法中发送邮件给指定的用户 * @see java.lang.Thread#run() */ @Override public void run() { try{ Properties prop = new Properties(); prop.setProperty("mail.host", host); prop.setProperty("mail.transport.protocol", "smtp"); prop.setProperty("mail.smtp.auth", "true"); Session session = Session.getInstance(prop); session.setDebug(true); Transport ts = session.getTransport(); ts.connect(host, username, password); Message message = createEmail(session,user); ts.sendMessage(message, message.getAllRecipients()); ts.close(); }catch (Exception e) { throw new RuntimeException(e); } } /** * @Method: createEmail * @Description: 创建要发送的邮件 * @Anthor:孤傲苍狼 * * @param session * @param user * @return * @throws Exception */ public Message createEmail(Session session,User user) throws Exception{ MimeMessage message = new MimeMessage(session); message.setFrom( new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); message.setSubject( "用户注册邮件" ); String info = "恭喜您注册成功,您的用户名:" + user.getUsername() + ",您的密码:" + user.getPassword() + ",请妥善保管,如有问题请联系网站客服!!" ; message.setContent(info, "text/html;charset=UTF-8" ); message.saveChanges(); return message; } } |
2.3、编写处理用户注册的Servlet
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
|
package me.gacl.web.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import me.gacl.domain.User; import me.gacl.service.UserService; public class RegisterServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String username = request.getParameter( "username" ); String password = request.getParameter( "password" ); String email = request.getParameter( "email" ); User user = new User(); user.setEmail(email); user.setPassword(password); user.setUsername(username); System.out.println( "把用户信息注册到数据库中" ); //用户注册成功之后就使用用户注册时的邮箱给用户发送一封Email //发送邮件是一件非常耗时的事情,因此这里开辟了另一个线程来专门发送邮件 Sendmail send = new Sendmail(user); //启动线程,线程启动之后就会执行run方法来发送邮件 send.start(); //注册用户 //new UserService().registerUser(user); request.setAttribute( "message" , "恭喜您,注册成功,我们已经发了一封带了注册信息的电子邮件,请查收,如果没有收到,可能是网络原因,过一会儿就收到了!!" ); request.getRequestDispatcher( "/message.jsp" ).forward(request, response); } catch (Exception e) { e.printStackTrace(); request.setAttribute( "message" , "注册失败!!" ); request.getRequestDispatcher( "/message.jsp" ).forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
程序运行效果如下:
现在很多网站都有这样的功能,用户注册完成之后,网站根据我们注册时填写的邮箱给我们一封Email,然后点击Email中的超链接去激活我们的用户。这种功能就是这样实现的。
在总结使用JavaMail发送邮件时发现,将邮件发送到sina或者sohu的邮箱时,不一定能够马上收取得到邮件,总是有延迟,有时甚至会延迟很长的时间,甚至会被当成垃圾邮件来处理掉,或者干脆就拒绝接收,有时候为了看到邮件发送成功的效果,要等半天,实属无奈啊。