|
|
@@ -11,12 +11,19 @@ import org.springframework.mail.javamail.JavaMailSenderImpl;
|
|
11
|
11
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
12
|
12
|
import org.springframework.stereotype.Component;
|
|
13
|
13
|
|
|
14
|
|
-import javax.mail.MessagingException;
|
|
15
|
|
-import javax.mail.internet.MimeMessage;
|
|
|
14
|
+import javax.activation.DataHandler;
|
|
|
15
|
+import javax.mail.*;
|
|
|
16
|
+import javax.mail.internet.*;
|
|
|
17
|
+import javax.mail.util.ByteArrayDataSource;
|
|
16
|
18
|
import javax.validation.constraints.NotNull;
|
|
17
|
19
|
import java.io.File;
|
|
18
|
20
|
import java.io.IOException;
|
|
|
21
|
+import java.nio.file.Files;
|
|
|
22
|
+import java.nio.file.Paths;
|
|
|
23
|
+import java.security.Security;
|
|
|
24
|
+import java.util.Date;
|
|
19
|
25
|
import java.util.List;
|
|
|
26
|
+import java.util.Properties;
|
|
20
|
27
|
import java.util.regex.Matcher;
|
|
21
|
28
|
import java.util.regex.Pattern;
|
|
22
|
29
|
|
|
|
@@ -40,15 +47,15 @@ public class EmailOutUtil {
|
|
40
|
47
|
// @Value("${spring.mail-out-network.from}")
|
|
41
|
48
|
// private static String fromOut;
|
|
42
|
49
|
@Value("${spring.mail.host}")
|
|
43
|
|
- private String hostOut;
|
|
|
50
|
+ private String hostOut;
|
|
44
|
51
|
@Value("${spring.mail.username}")
|
|
45
|
|
- private String usernameOut;
|
|
|
52
|
+ private String usernameOut;
|
|
46
|
53
|
@Value("${spring.mail.password}")
|
|
47
|
|
- private String passwordOut;
|
|
|
54
|
+ private String passwordOut;
|
|
48
|
55
|
@Value("${spring.mail.port}")
|
|
49
|
|
- private Integer portOut;
|
|
|
56
|
+ private Integer portOut;
|
|
50
|
57
|
|
|
51
|
|
- public JavaMailSender rebuildMailSender() {
|
|
|
58
|
+ public JavaMailSender rebuildMailSender() {
|
|
52
|
59
|
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
|
53
|
60
|
mailSender.setHost(hostOut);
|
|
54
|
61
|
mailSender.setUsername(usernameOut);
|
|
|
@@ -66,7 +73,7 @@ public class EmailOutUtil {
|
|
66
|
73
|
* @param subject 邮件主题
|
|
67
|
74
|
* @param content 邮件内容(发送内容)
|
|
68
|
75
|
*/
|
|
69
|
|
- public void sendMessage(String to, String subject, String content, String from, JavaMailSender mailSender) {
|
|
|
76
|
+ public void sendMessage(String to, String subject, String content, String from, JavaMailSender mailSender) {
|
|
70
|
77
|
// 创建一个邮件对象
|
|
71
|
78
|
SimpleMailMessage msg = new SimpleMailMessage();
|
|
72
|
79
|
msg.setFrom(from);
|
|
|
@@ -80,15 +87,88 @@ public class EmailOutUtil {
|
|
80
|
87
|
////System.out.println("发送成功:" + from + ":to:" + to);
|
|
81
|
88
|
}
|
|
82
|
89
|
|
|
|
90
|
+ /**
|
|
|
91
|
+ * @param to 收件人
|
|
|
92
|
+ * @param message 邮件内容
|
|
|
93
|
+ * @param subject 邮件主题
|
|
|
94
|
+ * @param fileList 邮件附件
|
|
|
95
|
+ */
|
|
|
96
|
+ public void sendEmil(String to, String message, String subject, List<File> fileList, File file) {
|
|
|
97
|
+ try {
|
|
|
98
|
+ Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
|
|
|
99
|
+ final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
|
|
|
100
|
+ //设置邮件会话参数
|
|
|
101
|
+ Properties props = new Properties();
|
|
|
102
|
+ //邮箱的发送服务器地址
|
|
|
103
|
+ props.setProperty("mail.smtp.host", "smtp.163.com");
|
|
|
104
|
+ props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
|
|
|
105
|
+ props.setProperty("mail.smtp.socketFactory.fallback", "false");
|
|
|
106
|
+ //邮箱发送服务器端口,这里设置为465端口
|
|
|
107
|
+ props.setProperty("mail.smtp.port", "465");
|
|
|
108
|
+ props.setProperty("mail.smtp.socketFactory.port", "465");
|
|
|
109
|
+ props.put("mail.smtp.auth", "true");
|
|
|
110
|
+ //获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm
|
|
|
111
|
+ Session session = Session.getDefaultInstance(props, new Authenticator() {
|
|
|
112
|
+ @Override
|
|
|
113
|
+ protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
114
|
+ return new PasswordAuthentication(usernameOut, passwordOut);
|
|
|
115
|
+ }
|
|
|
116
|
+ });
|
|
|
117
|
+ //通过会话,得到一个邮件,用于发送
|
|
|
118
|
+ Message msg = new MimeMessage(session);
|
|
|
119
|
+ //设置发件人
|
|
|
120
|
+ msg.setFrom(new InternetAddress(usernameOut));
|
|
|
121
|
+ //设置收件人,to为收件人,cc为抄送,bcc为密送
|
|
|
122
|
+ msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
|
|
|
123
|
+ msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(to, false));
|
|
|
124
|
+ msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to, false));
|
|
|
125
|
+ //设置邮件消息
|
|
|
126
|
+ msg.setSubject(subject);
|
|
|
127
|
+ msg.setText(message);
|
|
|
128
|
+ //设置发送的日期
|
|
|
129
|
+ msg.setSentDate(new Date());
|
|
|
130
|
+ // 创建邮件正文
|
|
|
131
|
+ MimeMultipart multipart = new MimeMultipart();
|
|
|
132
|
+// MimeBodyPart bodyPart = new MimeBodyPart();
|
|
|
133
|
+// bodyPart.setContent("This is the body of the email", "text/html");
|
|
|
134
|
+// multipart.addBodyPart(bodyPart);
|
|
|
135
|
+ // 添加附件
|
|
|
136
|
+ if (file != null) {
|
|
|
137
|
+ MimeBodyPart attachmentPart = new MimeBodyPart();
|
|
|
138
|
+ attachmentPart.attachFile(file);
|
|
|
139
|
+ attachmentPart.setFileName(MimeUtility.encodeText(file.getName()));
|
|
|
140
|
+ multipart.addBodyPart(attachmentPart);
|
|
|
141
|
+ //将multipart对象放入邮件
|
|
|
142
|
+ msg.setContent(multipart);
|
|
|
143
|
+ } else if (fileList != null && fileList.size() > 0) {
|
|
|
144
|
+ // 添加附件(多个)
|
|
|
145
|
+ if (fileList != null && fileList.size() > 0) {
|
|
|
146
|
+ for (File tempfile : fileList) {
|
|
|
147
|
+ MimeBodyPart attachmentPart = new MimeBodyPart();
|
|
|
148
|
+ attachmentPart.attachFile(tempfile);
|
|
|
149
|
+ attachmentPart.setFileName(MimeUtility.encodeText(tempfile.getName()));
|
|
|
150
|
+ multipart.addBodyPart(attachmentPart);
|
|
|
151
|
+ }
|
|
|
152
|
+ msg.setContent(multipart);
|
|
|
153
|
+ }
|
|
|
154
|
+ }
|
|
|
155
|
+ //调用Transport的send方法去发送邮件
|
|
|
156
|
+ Transport.send(msg);
|
|
|
157
|
+ } catch (Exception e) {
|
|
|
158
|
+ e.printStackTrace();
|
|
|
159
|
+ }
|
|
|
160
|
+
|
|
|
161
|
+ }
|
|
|
162
|
+
|
|
83
|
163
|
/**
|
|
84
|
164
|
* 发送带附件的邮件信息
|
|
85
|
165
|
*
|
|
86
|
|
- * @param to 接收方
|
|
87
|
|
- * @param subject 邮件主题
|
|
88
|
|
- * @param content 邮件内容(发送内容)
|
|
89
|
|
- * @param fileList 文件集合 // 可发送多个附件
|
|
|
166
|
+ * @param to 接收方
|
|
|
167
|
+ * @param subject 邮件主题
|
|
|
168
|
+ * @param content 邮件内容(发送内容)
|
|
|
169
|
+ * @param fileList 文件集合 // 可发送多个附件
|
|
90
|
170
|
*/
|
|
91
|
|
- public void sendMessageCarryFiles(String to, String subject, String content, List<File> fileList, String from, @NotNull JavaMailSender mailSender) {
|
|
|
171
|
+ public void sendMessageCarryFiles(String to, String subject, String content, List<File> fileList, String from, @NotNull JavaMailSender mailSender) {
|
|
92
|
172
|
MimeMessage mimeMessage = mailSender.createMimeMessage();
|
|
93
|
173
|
try {
|
|
94
|
174
|
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|