一 概念简介
Secure Socket Layer,为Netscape所研发,用以保障在Internet上数据传输之安全,利用数据加密(Encryption)技术,可确保数据在网络上之传输过程中不会被截取及窃听。一般通用之规格为40 bit之安全标准,美国则已推出128 bit之更高安全标准,但限制出境。只要3.0版本以上之I.E.或Netscape浏览器即可支持SSL。
当前版本为3.0。它已被广泛地用于Web浏览器与服务器之间的身份认证和加密数据传输。
(PS:来至百度百科)
二 在JavaMail中使用SSL对邮件发送进行加密
实际上大部分操作都跟普通的邮件发送是一样的,只是有两个地方有所变化。(1)传输端口从25改成465;(2)替换默认的socketFactory
下面我以163邮箱实现的SSL传输举例说明,下图是163邮箱官方给出的相关配置信息:
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
185
|
package
javamail.zifangsky.com;
import
java.io.UnsupportedEncodingException;
import
java.util.ArrayList;
import
java.util.Date;
import
java.util.List;
import
java.util.Properties;
import
javax.activation.DataHandler;
import
javax.activation.FileDataSource;
import
javax.mail.Address;
import
javax.mail.BodyPart;
import
javax.mail.Multipart;
import
javax.mail.Session;
import
javax.mail.Transport;
import
javax.mail.internet.InternetAddress;
import
javax.mail.internet.MimeBodyPart;
import
javax.mail.internet.MimeMessage;
import
javax.mail.internet.MimeMultipart;
import
javax.mail.internet.MimeUtility;
public
class
SendMailBySSL {
private
final
String SSL_FACTORY =
"javax.net.ssl.SSLSocketFactory"
;
private
String smtpServer;
// SMTP服务器地址
private
String port;
// 端口
private
String username;
// 登录SMTP服务器的用户名
private
String password;
// 登录SMTP服务器的密码
private
List<String> recipients =
new
ArrayList<String>();
// 收件人地址集合
private
String subject;
// 邮件主题
private
String content;
// 邮件正文
private
List<String> attachmentNames =
new
ArrayList<String>();
// 附件路径信息集合
public
SendMailBySSL() {
}
public
SendMailBySSL(String smtpServer, String port, String username,
String password, List<String> recipients, String subject,
String content, List<String> attachmentNames) {
this
.smtpServer = smtpServer;
this
.port = port;
this
.username = username;
this
.password = password;
this
.recipients = recipients;
this
.subject = subject;
this
.content = content;
this
.attachmentNames = attachmentNames;
}
public
void
setSmtpServer(String smtpServer) {
this
.smtpServer = smtpServer;
}
public
void
setPort(String port) {
this
.port = port;
}
public
void
setUsername(String username) {
this
.username = username;
}
public
void
setPassword(String password) {
this
.password = password;
}
public
void
setRecipients(List<String> recipients) {
this
.recipients = recipients;
}
public
void
setSubject(String subject) {
this
.subject = subject;
}
public
void
setContent(String content) {
this
.content = content;
}
public
void
setAttachmentNames(List<String> attachmentNames) {
this
.attachmentNames = attachmentNames;
}
/**
* 进行base64加密,防止中文乱码
* */
public
String changeEncode(String str) {
try
{
str = MimeUtility.encodeText(
new
String(str.getBytes(),
"UTF-8"
),
"UTF-8"
,
"B"
);
// "B"代表Base64
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
return
str;
}
/**
* 正式发邮件
* */
public
boolean
sendMail() {
Properties properties =
new
Properties();
properties.put(
"mail.smtp.host"
, smtpServer);
properties.put(
"mail.smtp.auth"
,
"true"
);
properties.put(
"mail.smtp.socketFactory.class"
, SSL_FACTORY);
//使用JSSE的SSL socketfactory来取代默认的socketfactory
properties.put(
"mail.smtp.socketFactory.fallback"
,
"false"
);
// 只处理SSL的连接,对于非SSL的连接不做处理
properties.put(
"mail.smtp.port"
, port);
properties.put(
"mail.smtp.socketFactory.port"
, port);
Session session = Session.getInstance(properties);
session.setDebug(
true
);
MimeMessage message =
new
MimeMessage(session);
try
{
// 发件人
Address address =
new
InternetAddress(username);
message.setFrom(address);
// 收件人
for
(String recipient : recipients) {
System.out.println(
"收件人:"
+ recipient);
Address toAddress =
new
InternetAddress(recipient);
message.setRecipient(MimeMessage.RecipientType.TO, toAddress);
// 设置收件人,并设置其接收类型为TO
/**
* TO:代表有健的主要接收者。 CC:代表有健的抄送接收者。 BCC:代表邮件的暗送接收者。
* */
}
// 主题
message.setSubject(changeEncode(subject));
// 时间
message.setSentDate(
new
Date());
Multipart multipart =
new
MimeMultipart();
// 添加文本
BodyPart text =
new
MimeBodyPart();
text.setText(content);
multipart.addBodyPart(text);
// 添加附件
for
(String fileName : attachmentNames) {
BodyPart adjunct =
new
MimeBodyPart();
FileDataSource fileDataSource =
new
FileDataSource(fileName);
adjunct.setDataHandler(
new
DataHandler(fileDataSource));
adjunct.setFileName(changeEncode(fileDataSource.getName()));
multipart.addBodyPart(adjunct);
}
// 清空收件人集合,附件集合
recipients.clear();
attachmentNames.clear();
message.setContent(multipart);
message.saveChanges();
}
catch
(Exception e) {
e.printStackTrace();
return
false
;
}
try
{
Transport transport = session.getTransport(
"smtp"
);
transport.connect(smtpServer, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch
(Exception e) {
e.printStackTrace();
return
false
;
}
return
true
;
}
public
static
void
main(String[] args) {
List<String> recipients =
new
ArrayList<String>();
// recipients.add("123456789@qq.com");
recipients.add(
"admin@zifangsky.cn"
);
String subject =
"这封邮件是为了测试SMTP的SSL加密传输"
;
String content =
"这是这封邮件的正文"
;
List<String> attachmentNames =
new
ArrayList<String>();
attachmentNames.add(
"C://Users//Administrator//Desktop//kali.txt"
);
SendMailBySSL sendMailBySSL =
new
SendMailBySSL(
"smtp.163.com"
,
"465"
,
"youname@163.com"
,
"youpassword"
, recipients, subject, content,
attachmentNames);
sendMailBySSL.sendMail();
}
}
|
三 测试结果如下
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1721164,如需转载请自行联系原作者