闲来没事研究了一下javamail发邮件。虽然比Spring框架的mail复杂一些,但是还是值得研究一下的。
mail.jar和本程序的jar包都会在附件中上传,供大家使用学习。
第一个类,用于密码验证,注意看注释,很重要。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package
com.xian.gmail;
import
javax.mail.Authenticator;
import
javax.mail.PasswordAuthentication;
/**
* JavaMail的密码验证器
* @author 贾小仙
*/
public
class
MyAuther
extends
Authenticator{
String userName=
null
;
String password=
null
;
public
MyAuther() {
}
public
MyAuther(String userName, String password) {
this
.userName = userName;
this
.password = password;
}
//getPasswordAuthentication 名字必须对。 源码中是return null。相当于重写
protected
PasswordAuthentication getPasswordAuthentication(){
return
new
PasswordAuthentication(userName,password);
}
}
|
第二个类是实体类,做一些基本参数处理
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
|
package
com.xian.gmail;
public
class
MailSenderInfo {
// 发送邮件的服务器的IP和端口
private
String host;
private
String port=
"25"
;
// 邮件发送者的地址
private
String address;
// 邮件接收者的地址
private
String toAddress;
// 登陆邮件发送服务器的用户名和密码
private
String userName;
private
String password;
// 是否需要身份验证
private
Boolean Validate=
true
;
// 邮件主题
private
String subJect;
// 邮件的文本内容
private
String context;
// 邮件附件的文件名
private
String[] fileName;
/**
* 获得邮件会话属性
*/
/* public Properties getProperties(){
Properties pro=new Properties();
pro.put("mail.smtp.host",this.host);
pro.put("mail.smtp.port", this.port);
pro.put("mail.smtp.auth", Validate ? "true":"false");
return pro;
}*/
public
String getHost() {
return
host;
}
public
void
setHost(String host) {
this
.host = host;
}
public
String getPort() {
return
port;
}
public
void
setPort(String port) {
this
.port = port;
}
public
String getAddress() {
return
address;
}
public
void
setAddress(String address) {
this
.address = address;
}
public
String getToAddress() {
return
toAddress;
}
public
void
setToAddress(String toAddress) {
this
.toAddress = toAddress;
}
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
Boolean IsValidate() {
return
Validate;
}
public
void
setValidate(Boolean validate) {
this
.Validate=validate;
}
public
String getSubJect() {
return
subJect;
}
public
void
setSubJect(String subJect) {
this
.subJect = subJect;
}
public
String getContext() {
return
context;
}
public
void
setContext(String context) {
this
.context = context;
}
public
String[] getFileName() {
return
fileName;
}
public
void
setFileName(String[] fileName) {
this
.fileName = fileName;
}
}
|
第三个类就是实现类了,发送邮件的具体操作都在这里
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
|
package
com.xian.gmail;
import
java.util.Date;
import
java.util.Properties;
import
javax.mail.BodyPart;
import
javax.mail.Message;
import
javax.mail.MessagingException;
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.Address;
import
javax.mail.Multipart;
/**
* 简单邮件(不带附件的邮件)发送器
*/
public
class
MailSender {
public
boolean
sendTextMail(MailSenderInfo mailInfo){
// 判断是否需要身份认证
MyAuther authenticator =
null
;
if
(mailInfo.IsValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator =
new
MyAuther(mailInfo.getUserName(), mailInfo.getPassword());
}
Properties pro=getProperties(mailInfo);
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try
{
// 根据session创建一个邮件消息
Message mailMessage =
new
MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from =
new
InternetAddress(mailInfo.getAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to =
new
InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubJect());
// 设置邮件消息发送的时间
mailMessage.setSentDate(
new
Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContext();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return
true
;
}
catch
(MessagingException ex) {
ex.printStackTrace();
}
return
false
;
}
/**
* 用HTML格式发送邮件
* @param mailInfo 邮件基本信息
* @return 成功
* @author 贾小仙
*/
public
boolean
sendHtmlMail(MailSenderInfo mailInfo){
MyAuther auth=
null
;
if
(mailInfo.IsValidate()){
auth=
new
MyAuther(mailInfo.getUserName(), mailInfo.getPassword());
}
Properties pro=getProperties(mailInfo);
try
{
Session sendMailSession = Session.getDefaultInstance(pro, auth);
Message msg =
new
MimeMessage(sendMailSession);
Address address =
new
InternetAddress(mailInfo.getAddress());
msg.setFrom(address);
Address toAddress=
new
InternetAddress(mailInfo.getToAddress());
msg.setRecipient(Message.RecipientType.TO, toAddress);
msg.setSubject(mailInfo.getSubJect());
msg.setSentDate(
new
Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mailPart=
new
MimeMultipart();
BodyPart html =
new
MimeBodyPart();
html.setContent(mailInfo.getContext(),
"text/html;charset=utf-8"
);
mailPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
msg.setContent(mailPart);
// 发送邮件
Transport.send(msg);
return
true
;
}
catch
(MessagingException e) {
// TODO: handle exception
e.printStackTrace();
}
return
false
;
}
public
Properties getProperties(MailSenderInfo mailInfo){
Properties pro =
new
Properties();
String mailName=mailInfo.getUserName();
String mailType=mailName.substring(mailName.indexOf(
'@'
)+
1
, mailName.length());
if
(mailType.indexOf(
"gmail"
)!=-
1
){
pro.setProperty(
"mail.smtp.host"
,
"smtp.gmail.com"
);
pro.setProperty(
"mail.smtp.port"
,
"465"
);
pro.setProperty(
"mail.smtp.auth"
,
"true"
);
pro.setProperty(
"mail.smtp.socketFactory.class"
,
"javax.net.ssl.SSLSocketFactory"
);
pro.setProperty(
"mail.smtp.socketFactory.fallback"
,
"false"
);
pro.setProperty(
"mail.smtp.socketFactory.port"
,
"465"
);
pro.setProperty(
"mail.smtp.ssl"
,
"true"
);
}
if
(mailType.equals(
"163.com"
)||mailType.equals(
"sohu.com"
)||mailType.equals(
"126.com"
)){
String mailserver =
"smtp."
+mailType;
pro.setProperty(
"mail.smtp.host"
, mailserver);
pro.setProperty(
"mail.smtp.port"
,
"25"
);
pro.setProperty(
"mail.smtp.auth"
,
"true"
);
}
if
(mailType.indexOf(
"sina"
)!=-
1
){
pro.setProperty(
"mail.smtp.host"
,
"smtp.sina.com.cn"
);
pro.setProperty(
"mail.smtp.port"
,
"25"
);
pro.setProperty(
"mail.smtp.auth"
,
"true"
);
}
return
pro;
}
}
|
第四个类 相当于一个接口类,用来传参数。
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
|
package
com.xian.gmail;
public
class
Send {
/**
* 发送邮件所需要的参数 用户名,密码,收件人,主题,内容
* @param userName
* @param password
* @param toAddress
* @param subJect
* @param context
*/
public
static
void
sendMail(String userName,String password,String toAddress,String subJect,String context){
MailSenderInfo mailInfo=
new
MailSenderInfo();
mailInfo.setUserName(userName);
mailInfo.setPassword(password);
mailInfo.setAddress(userName);
mailInfo.setToAddress(toAddress);
mailInfo.setSubJect(subJect);
mailInfo.setContext(context);
MailSender send=
new
MailSender();
//这个类主要来发送邮件
boolean
result=send.sendTextMail(mailInfo);
//发送文体格式
if
(result){
System.out.println(
"发送成功"
);
}
else
{
System.out.println(
"发送失败"
);
}
};
}
|
第五个类 具体调用
1
2
3
4
5
6
7
8
|
import
com.xian.gmail.Send;
public
class
GoMail
extends
Send{
public
void
main(){
}
public
void
main(String[] args) {
Send.sendMail(
"*****@163.com"
,
"*****"
,
"***@163.com"
,
"你好呀"
,
"Can you speek English?Mr 小贾"
);
}
}
|
简单的实现,通用126,163,sohu,sina,gmail。 qq忘记了自己添加