-
Notifications
You must be signed in to change notification settings - Fork 1
/
EmailUtil.java
142 lines (129 loc) · 4.17 KB
/
EmailUtil.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
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
package cn.xanderye.util;
import lombok.extern.slf4j.Slf4j;
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.MimeMessage;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
/**
* @author XanderYe
* @description:
* @date 2021/4/22 13:40
*/
@Slf4j
public class EmailUtil {
/**
* 配置对象
*/
private static Properties mailConfig;
/**
* 用户名
*/
private static String username = null;
/**
* 密码
*/
private static String password = null;
static {
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = classLoader.getResourceAsStream("email.properties");
if (is != null) {
mailConfig = new Properties();
mailConfig.load(is);
username = mailConfig.getProperty("mail.username");
password = mailConfig.getProperty("mail.password");
} else {
log.warn("Could not found email.properties, please configure manually.");
}
} catch (Exception e) {
log.error("Error initializing the config.");
}
}
/**
* 手动配置邮箱
* @param properties
* @return void
* @author yezhendong
* @date 2021/12/16
*/
public static void setConfig(Properties properties) {
mailConfig = properties;
}
/**
* 发送邮件给一个地址
* @param title
* @param content
* @param toEmail
* @return void
* @author XanderYe
* @date 2021/4/22
*/
public static void sendSimpleEmail(String title, String content, String toEmail) throws MessagingException {
Session session = Session.getInstance(mailConfig);
Message msg = new MimeMessage(session);
// 标题
msg.setSubject(title);
// 内容
msg.setText(content);
// 发件人
msg.setFrom(new InternetAddress(username));
// 收件人
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(toEmail));
msg.saveChanges();
Transport transport = session.getTransport();
transport.connect(username,password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
/**
*
* @param title
* @param content
* @param toEmailList 收件人
* @param carbonCopyList 抄送
* @return void
* @author XanderYe
* @date 2021/6/18
*/
public static void sendEmail(String title, String content, List<String> toEmailList, List<String> carbonCopyList) throws MessagingException {
if (toEmailList == null || toEmailList.isEmpty()) {
throw new RuntimeException("接收人不为空");
}
Session session = Session.getInstance(mailConfig);
Message msg = new MimeMessage(session);
InternetAddress[] addressesTo = new InternetAddress[toEmailList.size()];
for (int i = 0; i < toEmailList.size(); i++) {
addressesTo[i] = new InternetAddress(toEmailList.get(i));
}
InternetAddress[] addressesCc = null;
if (carbonCopyList != null && !carbonCopyList.isEmpty()) {
addressesCc = new InternetAddress[carbonCopyList.size()];
for (int i = 0; i < carbonCopyList.size(); i++) {
addressesCc[i] = new InternetAddress(carbonCopyList.get(i));
}
}
// 标题
msg.setSubject(title);
// 内容
msg.setText(content);
// 发件人
msg.setFrom(new InternetAddress(username));
// 收件人
msg.setRecipients(Message.RecipientType.TO, addressesTo);
// 抄送
if (addressesCc != null) {
msg.setRecipients(Message.RecipientType.CC, addressesCc);
}
msg.saveChanges();
Transport transport = session.getTransport();
transport.connect(username, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
}