1 package util;
2 import java.io.File;
3 import java.io.FileOutputStream;
4 import java.util.Properties;
5 import javax.activation.DataHandler;
6 import javax.activation.FileDataSource;
7 import javax.mail.Message;
8 import javax.mail.Session;
9 import javax.mail.Transport;
10 import javax.mail.internet.*;
11
12 /**
13 * @author tian
14 * @date 2019/4/2913:48
15 */
16 public class JavaMailUtil {
17
18 public static class MailBean{
19 private File[] files;
20 private String host;
21 private String protocol;
22 private Boolean auth;
23 private String userName;
24 private String password;
25 private String fromUser;
26 private String toUser;
27 private String titel;
28 private String content;
29 private Integer type;
30
31 public Integer getType() {
32 return type;
33 }
34
35 public MailBean setType(Integer type) {
36 this.type = type;
37 return this;
38 }
39
40 public File[] getFiles() {
41 return files;
42 }
43
44 public MailBean setFiles(File[] files) {
45 this.files = files;
46 return this;
47 }
48
49 public String getHost() {
50 return host;
51 }
52
53 public MailBean setHost(String host) {
54 this.host = host;
55 return this;
56 }
57
58 public String getProtocol() {
59 return protocol;
60 }
61
62 public MailBean setProtocol(String protocol) {
63 this.protocol = protocol;
64 return this;
65 }
66
67 public Boolean getAuth() {
68 return auth;
69 }
70
71 public MailBean setAuth(Boolean auth) {
72 this.auth = auth;
73 return this;
74 }
75
76 public String getUserName() {
77 return userName;
78 }
79
80 public MailBean setUserName(String userName) {
81 this.userName = userName;
82 return this;
83 }
84
85 public String getPassword() {
86 return password;
87 }
88
89 public MailBean setPassword(String password) {
90 this.password = password;
91 return this;
92 }
93
94 public String getFromUser() {
95 return fromUser;
96 }
97
98 public MailBean setFromUser(String fromUser) {
99 this.fromUser = fromUser;
100 return this;
101 }
102
103 public String getToUser() {
104 return toUser;
105 }
106
107 public MailBean setToUser(String toUser) {
108 this.toUser = toUser;
109 return this;
110 }
111
112 public String getTitel() {
113 return titel;
114 }
115
116 public MailBean setTitel(String titel) {
117 this.titel = titel;
118 return this;
119 }
120
121 public String getContent() {
122 return content;
123 }
124
125 public MailBean setContent(String content) {
126 this.content = content;
127 return this;
128 }
129 }
130 /**
131 * 发送简单的文字邮件
132 */
133 public synchronized static void Sendmail(MailBean mailBean) throws Exception {
134 Properties prop = new Properties();
135 prop.setProperty("mail.host", mailBean.getHost());
136 prop.setProperty("mail.transport.protocol", mailBean.getProtocol());
137 prop.setProperty("mail.smtp.auth", "true");
138 //使用JavaMail发送邮件的5个步骤
139 //1、创建session
140 Session session = Session.getInstance(prop);
141 //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
142 session.setDebug(true);
143 //2、通过session得到transport对象
144 Transport ts = session.getTransport();
145 //3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
146 ts.connect(mailBean.getHost(), mailBean.getUserName(), mailBean.getPassword());
147 //4、创建邮件
148 Message message=null;
149 if(mailBean.getType()==1){
150 message = createSimpleMail(session,mailBean);
151 }
152 if(mailBean.getType()==2){
153 message = createAttachMail(session,mailBean);
154 }
155
156 //5、发送邮件
157 ts.sendMessage(message, message.getAllRecipients());
158 ts.close();
159 }
160
161 /**
162 * @Method: createSimpleMail
163 * @Description: 创建一封只包含文本的邮件
164 * @Anthor:孤傲苍狼
165 *
166 * @param session
167 * @return
168 * @throws Exception
169 */
170 public static MimeMessage createSimpleMail(Session session,MailBean mailBean)
171 throws Exception {
172 //创建邮件对象
173 MimeMessage message = new MimeMessage(session);
174 //指明邮件的发件人
175 message.setFrom(new InternetAddress(mailBean.getFromUser()));
176 //指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
177 message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailBean.getToUser()));
178 //邮件的标题
179 message.setSubject(mailBean.getTitel());
180 //邮件的文本内容
181 message.setContent(mailBean.getContent(), "text/html;charset=UTF-8");
182 //返回创建好的邮件对象
183 return message;
184 }
185 /**
186 * @Method: createImageMail
187 * @Description: 生成一封邮件正文带图片的邮件
188 * @Anthor:孤傲苍狼
189 *
190 * @param session
191 * @return
192 * @throws Exception
193 */
194 public static MimeMessage createImageMail(Session session,MailBean mailBean) throws Exception {
195 //创建邮件对象
196 MimeMessage message = new MimeMessage(session);
197 //指明邮件的发件人
198 message.setFrom(new InternetAddress(mailBean.getFromUser()));
199 //指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
200 message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailBean.getToUser()));
201 //邮件标题
202 message.setSubject(mailBean.getTitel());
203
204 // 准备邮件数据
205 // 准备邮件正文数据
206 MimeBodyPart text = new MimeBodyPart();
207 text.setContent(mailBean.getContent(), "text/html;charset=UTF-8");
208 // 准备图片数据
209 MimeBodyPart image = new MimeBodyPart();
210 DataHandler dh = new DataHandler(new FileDataSource(mailBean.getFiles()[0]));
211 image.setDataHandler(dh);
212 image.setContentID("xxx.jpg");
213 // 描述数据关系
214 MimeMultipart mm = new MimeMultipart();
215 mm.addBodyPart(text);
216 mm.addBodyPart(image);
217 mm.setSubType("related");
218
219 message.setContent(mm);
220 message.saveChanges();
221 //将创建好的邮件写入到E盘以文件的形式进行保存
222 message.writeTo(new FileOutputStream("E:\\ImageMail.eml"));
223 //返回创建好的邮件
224 return message;
225 }
226
227 /**
228 * @Method: createAttachMail
229 * @Description: 创建一封带附件的邮件
230 * @Anthor:孤傲苍狼
231 *
232 * @param session
233 * @return
234 * @throws Exception
235 */
236 public static MimeMessage createAttachMail(Session session ,MailBean mailBean) throws Exception{
237 MimeMessage message = new MimeMessage(session);
238
239 //设置邮件的基本信息
240 //发件人
241 message.setFrom(new InternetAddress(mailBean.getFromUser()));
242 //收件人
243 message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailBean.getToUser()));
244 //邮件标题
245 message.setSubject(mailBean.getTitel());
246
247 //创建邮件正文,为了避免邮件正文中文乱码问题,需要使用charset=UTF-8指明字符编码
248 MimeBodyPart text = new MimeBodyPart();
249 text.setContent(mailBean.getContent(), "text/html;charset=UTF-8");
250
251 if(mailBean.getFiles().length!=0){
252 //创建邮件附件
253 MimeBodyPart attach = new MimeBodyPart();
254 DataHandler dh = new DataHandler(new FileDataSource(mailBean.getFiles()[0]));
255 attach.setDataHandler(dh);
256 attach.setFileName(dh.getName()); //
257
258 //创建容器描述数据关系
259 MimeMultipart mp = new MimeMultipart();
260 mp.addBodyPart(text);
261 mp.addBodyPart(attach);
262 mp.setSubType("mixed");
263 message.setContent(mp);
264 message.saveChanges();
265 //将创建的Email写入到E盘存储
266 message.writeTo(new FileOutputStream("E:\\attachMail.eml"));
267
268 }
269 //返回生成的邮件
270 return message;
271
272 }
273
274 /**
275 * @Method: createMixedMail
276 * @Description: 生成一封带附件和带图片的邮件
277 * @Anthor:孤傲苍狼
278 *
279 * @param session
280 * @return
281 * @throws Exception
282 */
283 public static MimeMessage createMixedMail(Session session,MailBean mailBean) throws Exception {
284 //创建邮件
285 MimeMessage message = new MimeMessage(session);
286
287 //设置邮件的基本信息
288 message.setFrom(new InternetAddress(mailBean.getFromUser()));
289 message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailBean.getToUser()));
290 message.setSubject(mailBean.getTitel());
291
292 //正文
293 MimeBodyPart text = new MimeBodyPart();
294 text.setContent(mailBean.getContent(),"text/html;charset=UTF-8");
295
296 //附件1
297 MimeBodyPart attach = new MimeBodyPart();
298 DataHandler dh = new DataHandler(new FileDataSource(mailBean.getFiles()[0]));
299 attach.setDataHandler(dh);
300 attach.setFileName(dh.getName());
301
302
303 //描述关系:正文和图片
304 MimeMultipart mp1 = new MimeMultipart();
305 mp1.addBodyPart(text);
306 mp1.setSubType("related");
307
308 //描述关系:正文和附件
309 MimeMultipart mp2 = new MimeMultipart();
310 mp2.addBodyPart(attach);
311
312 //代表正文的bodypart
313 MimeBodyPart content = new MimeBodyPart();
314 content.setContent(mp1);
315 mp2.addBodyPart(content);
316 mp2.setSubType("mixed");
317
318 message.setContent(mp2);
319 message.saveChanges();
320
321 message.writeTo(new FileOutputStream("E:\\MixedMail.eml"));
322 //返回创建好的的邮件
323 return message;
324
325 }
326
327 public static void main(String[] args) throws Exception {
328 String code="3869";
329 String company="睿牛逼科技无限公司";
330 // User user=new User();
331 MailBean mailBean=new JavaMailUtil.MailBean();
332 mailBean.setAuth(true)
333 .setHost("smtp.qq.com")
334 .setProtocol("smtp")
335 .setType(2)
336 .setContent("<!DOCTYPE html>\n" +
337 "<html>\n" +
338 "<head>\n" +
339 "<title></title>\n" +
340 "<style>\n" +
341 ".qmbox {\n" +
342 "\tpadding: 0;\n" +
343 "}\n" +
344 ".qm_con_body_content {\n" +
345 "\theight: auto;\n" +
346 "\tmin-height: 100px;\n" +
347 "\t_height: 100px;\n" +
348 "\tword-wrap: break-word;\n" +
349 "\tfont-size: 14px;\n" +
350 "\tfont-family: \"lucida Grande\", Verdana, \"Microsoft YaHei\";\n" +
351 "}\n" +
352 ".body {\n" +
353 "\tline-height: 170%;\n" +
354 "}\n" +
355 "BODY {\n" +
356 "\tfont-family: \"lucida Grande\", Verdana, \"Microsoft YaHei\";\n" +
357 "\tfont-size: 12px;\n" +
358 "\t-webkit-font-smoothing: subpixel-antialiased;\n" +
359 "}\n" +
360 "BODY {\n" +
361 "\tmargin: 0;\n" +
362 "\tpadding: 0;\n" +
363 "}\n" +
364 "BODY {\n" +
365 "\tbackground-color: #fff;\n" +
366 "\tfont-size: 12px;\n" +
367 "}\n" +
368 "BODY {\n" +
369 "\tbackground: #fff;\n" +
370 "}\n" +
371 "BODY {\n" +
372 "\tbackground: #fff;\n" +
373 "\tcolor: #000;\n" +
374 "\tfont-weight: normal;\n" +
375 "\tfont-family: \"lucida Grande\", Verdana, \"Microsoft YaHei\";\n" +
376 "\tpadding: 0 7px 6px 4px;\n" +
377 "\tmargin: 0;\n" +
378 "}\n" +
379 "HTML {\n" +
380 "\ttop: 0px;\n" +
381 "}\n" +
382 ".body P {\n" +
383 "\tline-height: 170%;\n" +
384 "}\n" +
385 "</style>\n" +
386 "</head>\n" +
387 "\n" +
388 "<BODY mu=\"mu\" md=\"md\" module=\"qmReadMail\" context=\"ZC1912-rQ7uXSy7P7HThxdLFCOuY92\"><DIV class=mailcontainer id=qqmail_mailcontainer>\n" +
389 "<DIV id=mainmail style=\"MARGIN-BOTTOM: 12px; POSITION: relative; Z-INDEX: 1\">\n" +
390 "<DIV class=body id=contentDiv style=\"FONT-SIZE: 14px; HEIGHT: auto; POSITION: relative; ZOOM: 1; PADDING-BOTTOM: 10px; PADDING-TOP: 15px; PADDING-LEFT: 15px; Z-INDEX: 1; LINE-HEIGHT: 1.7; PADDING-RIGHT: 15px\" οnmοuseοver=getTop().stopPropagation(event); οnclick=\"getTop().preSwapLink(event, 'html', 'ZC1912-rQ7uXSy7P7HThxdLFCOuY92');\">\n" +
391 "<DIV id=qm_con_body>\n" +
392 "<DIV class=\"qmbox qm_con_body_content qqmail_webmail_only\" id=mailContentContainer>\n" +
393 "<DIV class=main style=\"OVERFLOW: hidden; WIDTH: 100%; BACKGROUND-COLOR: #f7f7f7\">\n" +
394 "<DIV class=content style=\"BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; BACKGROUND: #ffffff; BORDER-BOTTOM: #cccccc 1px solid; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; PADDING-LEFT: 25px; BORDER-LEFT: #cccccc 1px solid; MARGIN: 50px; PADDING-RIGHT: 25px\">\n" +
395 "<DIV class=header style=\"MARGIN-BOTTOM: 30px\">\n" +
396 "<P>亲爱的用户:</P></DIV>\n" +
397 "<P>您好!您正在进行邮箱验证,本次请求的验证码为:</P>\n" +
398 "<P><SPAN style=\"FONT-SIZE: 18px; FONT-WEIGHT: bold; COLOR: #f90\">"+code+"</SPAN><SPAN style=\"COLOR: #000000\">(为了保障您帐号的安全性,请在10分钟内完成验证)</SPAN></P>\n" +
399 "<DIV class=footer style=\"MARGIN-TOP: 30px\">\n" +
400 "<P>"+company+"</P>\n" +
401 "<P><SPAN style=\"BORDER-BOTTOM: #ccc 1px dashed; POSITION: relative; _display: inline-block\" t=\"5\" times=\"\" isout=\"0\">2019-02-12</SPAN></P></DIV>\n" +
402 "<DIV class=tip style=\"COLOR: #cccccc; TEXT-ALIGN: center\">该邮件为系统自动发送,请勿进行回复 </DIV></DIV></DIV></DIV></DIV></DIV></DIV></DIV></BODY>\n" +
403 "</html>\n")
404 .setFromUser("tian.**.1995@qq.com")
405 .setToUser("510002**94@qq.com")
406 .setTitel("邮箱验证")
407 .setUserName("tian.**.1995@qq.com")
408 .setPassword("lbcrovywudilebje")
409 .setFiles(new File[]{new File("E:/nginx-1.14.2.zip")});
410
411 Sendmail(mailBean);
412 }
413
414 }