UtilEmail.java
4.17 KB
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
package com.taover.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class UtilEmail {
public static Map<String, String> GLOBAL_CONFIG = new HashMap<String, String>(){{
put("host", "smtp.taover.com");
put("port", "25");
put("username", "taover-robot@taover.com");
put("password", "Lexi@1798");
put("header", "报警-服务不可用");
put("from", "taover-robot@taover.com");
put("fromName", "八爪云-服务监控");
put("htmlTopic", "text/html; charset=utf-8");
}};
/**
* 发�?�HTML格式的邮�?
*/
public static void sendHtmlMail(List<String> toList, String subject, String htmlContent, Map<String, String> mailHostConfig) {
// 获取系统环境
Properties prop = new Properties();
// 添加必要的信�?
prop.put("mail.smtp.host", getValue("host", mailHostConfig));
prop.put("mail.smtp.port", getValue("port", mailHostConfig));
prop.put("mail.smtp.auth", "true");
// 设置对话和邮件服务器进行通讯
Authenticator auth = new SimpleAuthenticator(getValue("username", mailHostConfig), getValue("password", mailHostConfig));
Session session = Session.getDefaultInstance(prop, auth);
// 设置邮件对象
Message message = new MimeMessage(session);
try
{
// 设置邮件主题
message.setSubject(subject);
// 设置邮件标题
message.setHeader("Header", getValue("header", mailHostConfig));
// 设置发�?�时�?
message.setSentDate(new Date());
// 设置发信人地�? �? 名字
Address address = new InternetAddress(getValue("from", mailHostConfig), getValue("fromName", mailHostConfig));
// 把发件人信息添加到信息中
message.setFrom(address);
// 设置收件人地�?
List<Address> addressList = new ArrayList<Address>();
for(String item: toList) {
if(item != null && !"".equals(item.trim())) {
addressList.add(new InternetAddress(item));
}
}
// 设置接收人地�?
message.addRecipients(Message.RecipientType.TO, addressList.toArray(new Address[addressList.size()]));
// 设置多个收件人地�?
// message.addRecipient(Message.RecipientType.TO,new InternetAddress("xxx@xxx.com"));
// 设置发�?�信息的内容 下面为发送hmml
// 设置邮件格式
message.setContent(htmlContent, getValue("htmlTopic", mailHostConfig));
// 保存上面添加的信�?
message.saveChanges();
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void sendHtmlMail(List<String> toList, String subject, String htmlContent) {
sendHtmlMail(toList, subject, htmlContent, null);
}
public static String getValue(String key, Map<String, String> localConfig) {
if(localConfig == null) {
return GLOBAL_CONFIG.get(key);
}else {
String data = localConfig.get(key);
if(data == null) {
return GLOBAL_CONFIG.get(key);
}else {
return data;
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
sendHtmlMail(Arrays.asList("wangbin@taover.com", "317058383@qq.com"), "测试邮件", "<h1>Hello Email Saturday V2</h1>");
}
}
class SimpleAuthenticator extends Authenticator{
private String username="";
private String password="";
public SimpleAuthenticator() {
super();
}
/**
* 设置验证的用户名和密�?
*/
public SimpleAuthenticator(String userName , String password) {
super();
this.username = userName;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username,this.password);
}
}