这里以一个用户注册+邮箱激活账号系统来演示传统的方式邮箱方式。
邮箱配置文件:
1
2
3
4
5
6
|
host=smtp.
163
.com
uname=
15581737164
pwd=xxxxx
//这里是邮箱的授权码,不是登录密码
from=
15511111111
@163
.com
subject=这是来自
15511111111
@163
.com邮箱的激活文件
content=<a href\=
"http\://localhost\:8080/User_login_regist_exit/user_active.action?code\={0}"
>点击这里完成激活</a>
|
1
|
code\={
0
}:是一个占位符
|
注册的web层代码:
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
|
public
String regist(){
HttpServletRequest request=ServletActionContext.getRequest();
Map<String, String> errors=
new
HashMap<String, String>();
user.setCode(CommonUtils.uuid()+CommonUtils.uuid());
user.setState(
false
);
//检查用户名
String username=user.getUsername();
if
(username ==
null
|| username.trim().isEmpty()) {
errors.put(
"username"
,
"用户名不能为空!"
);
}
else
if
(username.length() <
3
|| username.length() >
10
) {
errors.put(
"username"
,
"用户名长度必须在3~10之间!"
);
}
//检查密码
String password = user.getPassword();
if
(password ==
null
|| password.trim().isEmpty()) {
errors.put(
"password"
,
"密码不能为空!"
);
}
else
if
(password.length() <
3
|| password.length() >
10
) {
errors.put(
"password"
,
"密码长度必须在3~10之间!"
);
}
//检查邮箱
String email = user.getEmail();
if
(email ==
null
|| email.trim().isEmpty()) {
errors.put(
"email"
,
"Email不能为空!"
);
}
else
if
(!email.matches(
"\\w+@\\w+\\.\\w+"
)) {
errors.put(
"email"
,
"Email格式错误!"
);
}
if
(errors.size() >
0
) {
request.setAttribute(
"errors"
, errors);
request.setAttribute(
"form"
, user);
return
"regist"
;
}
//进行数据库操作
try
{
userService.regist(user);
}
catch
(UserException e) {
request.setAttribute(
"msg"
, e.getMessage());
request.setAttribute(
"form"
, user);
return
"regist"
;
}
//发送邮件
Properties props =
new
Properties();
try
{
props.load(
this
.getClass().getClassLoader()
.getResourceAsStream(
"email_template.properties"
));
}
catch
(IOException e1) {
e1.printStackTrace();
}
String host = props.getProperty(
"host"
);
//获取服务器主机
String uname = props.getProperty(
"uname"
);
//获取用户名
String pwd = props.getProperty(
"pwd"
);
//获取密码
String from = props.getProperty(
"from"
);
//获取发件人
String to = user.getEmail();
//获取收件人
String subject = props.getProperty(
"subject"
);
//获取主题
String content = props.getProperty(
"content"
);
//获取邮件内容
content = MessageFormat.format(content, user.getCode());
//替换{0}
Session session=MailUtils.createSession(host, uname, pwd);
Mail mail=
new
Mail(from,to,subject,content);
try
{
MailUtils.send(session, mail);
}
catch
(MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute(
"msg"
,
"恭喜,注册成功,请马上到邮箱激活"
);
return
"registsuccess"
;
}
|
已经编写好的utils类
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
|
public
class
Mail {
private
String from;
//发件人
private
StringBuilder toAddress =
new
StringBuilder();
//收件人
private
StringBuilder ccAddress =
new
StringBuilder();
//抄送
private
StringBuilder bccAddress =
new
StringBuilder();
//暗送
private
String subject;
//主题
private
String content;
//正文
// 附件列表
private
List<AttachBean> attachList =
new
ArrayList<AttachBean>();
public
Mail() {}
public
Mail(String from, String to) {
this
(from, to,
null
,
null
);
}
public
Mail(String from, String to, String subject, String content) {
this
.from = from;
this
.toAddress.append(to);
this
.subject = subject;
this
.content = content;
}
/**
* 返回发件人
* @return
*/
public
void
setFrom(String from) {
this
.from = from;
}
/**
* 返回发件人
* @return
*/
public
String getFrom() {
return
from;
}
/**
* 返回主题
*/
public
String getSubject() {
return
subject;
}
/**
* 设置主题
*/
public
void
setSubject(String subject) {
this
.subject = subject;
}
/**
* 获取主题内容
*/
public
String getContent() {
return
content;
}
/**
* 设置主题内容
* @param content
*/
public
void
setContent(String content) {
this
.content = content;
}
/**
* 获取收件人
* @return
*/
public
String getToAddress() {
return
toAddress.toString();
}
/**
* 获取抄送
* @return
*/
public
String getCcAddress() {
return
ccAddress.toString();
}
/**
* 获取暗送
* @return
*/
public
String getBccAddress() {
return
bccAddress.toString();
}
/**
* 添加收件人,可以是多个收件人
* @param to
*/
public
void
addToAddress(String to) {
if
(
this
.toAddress.length() >
0
) {
this
.toAddress.append(
","
);
}
this
.toAddress.append(to);
}
/**
* 添加抄送人,可以是多个抄送人
* @param cc
*/
public
void
addCcAddress(String cc) {
if
(
this
.ccAddress.length() >
0
) {
this
.ccAddress.append(
","
);
}
this
.ccAddress.append(cc);
}
/**
* 添加暗送人,可以是多个暗送人
* @param bcc
*/
public
void
addBccAddress(String bcc) {
if
(
this
.bccAddress.length() >
0
) {
this
.bccAddress.append(
","
);
}
this
.bccAddress.append(bcc);
}
/**
* 添加附件,可以添加多个附件
* @param attachBean
*/
public
void
addAttach(AttachBean attachBean) {
this
.attachList.add(attachBean);
}
/**
* 获取所有附件
* @return
*/
public
List<AttachBean> getAttachs() {
return
this
.attachList;
}
}
|
MailUtils
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
|
public
class
MailUtils {
public
static
Session createSession(String host,
final
String username,
final
String password) {
Properties prop =
new
Properties();
prop.setProperty(
"mail.host"
, host);
// 指定主机
prop.setProperty(
"mail.smtp.auth"
,
"true"
);
// 指定验证为true
// 创建验证器
Authenticator auth =
new
Authenticator() {
public
PasswordAuthentication getPasswordAuthentication() {
return
new
PasswordAuthentication(username, password);
}
};
// 获取session对象
return
Session.getInstance(prop, auth);
}
/**
* 发送指定的邮件
*
* @param mail
*/
public
static
void
send(Session session,
final
Mail mail)
throws
MessagingException,
IOException {
MimeMessage msg =
new
MimeMessage(session);
// 创建邮件对象
msg.setFrom(
new
InternetAddress(mail.getFrom()));
// 设置发件人
msg.addRecipients(RecipientType.TO, mail.getToAddress());
// 设置收件人
// 设置抄送
String cc = mail.getCcAddress();
if
(!cc.isEmpty()) {
msg.addRecipients(RecipientType.CC, cc);
}
// 设置暗送
String bcc = mail.getBccAddress();
if
(!bcc.isEmpty()) {
msg.addRecipients(RecipientType.BCC, bcc);
}
msg.setSubject(mail.getSubject());
// 设置主题
MimeMultipart parts =
new
MimeMultipart();
// 创建部件集对象
MimeBodyPart part =
new
MimeBodyPart();
// 创建一个部件
part.setContent(mail.getContent(),
"text/html;charset=utf-8"
);
// 设置邮件文本内容
parts.addBodyPart(part);
// 把部件添加到部件集中
///////////////////////////////////////////
// 添加附件
List<AttachBean> attachBeanList = mail.getAttachs();
// 获取所有附件
if
(attachBeanList !=
null
) {
for
(AttachBean attach : attachBeanList) {
MimeBodyPart attachPart =
new
MimeBodyPart();
// 创建一个部件
attachPart.attachFile(attach.getFile());
// 设置附件文件
attachPart.setFileName(MimeUtility.encodeText(attach
.getFileName()));
// 设置附件文件名
String cid = attach.getCid();
if
(cid !=
null
) {
attachPart.setContentID(cid);
}
parts.addBodyPart(attachPart);
}
}
msg.setContent(parts);
// 给邮件设置内容
Transport.send(msg);
// 发邮件
}
}
|
版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任
本文转自 叫我北北 51CTO博客,原文链接:http://blog.51cto.com/qinbin/1966857