EmilUtils.java
4.92 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
136
137
/*
* Copyright (c) 2018, 2028, glsx and/or its affiliates. All rights reserved.
* GLSX PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package com.glsx.task.utils;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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 com.glsx.common.utils.PropertiesUtil;
/**
* @ClassName: EmilUtils
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Zheng Xiajun
* @date 2020年3月24日
*
*/
public class EmilUtils {
public final static Session EMAIL_SESSION = Session.getDefaultInstance(new Properties() {
private static final long serialVersionUID = 7930835613494309306L;
{
this.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
this.setProperty("mail.smtp.socketFactory.fallback", "false");
this.setProperty("mail.smtp.auth", "true");
}
});
// 邮箱已开通,帐号:bhgreport@didihu.com.cn初始密码为:Gsx%2019
public final static String EMAIL_FROM_HOST = "smtphz.qiye.163.com";
public final static int EMAIL_FROM_PORT = 465;
public final static String EMAIL_FROM_ACCOUNT = "bhgreport@didihu.com.cn";
public final static String EMAIL_FROM_PASSWORD = "Gsx%2019";
// public final static String EMAIL_TO_ACCOUNT = "zhengxj@qurongbao.com.cn";
// public final static String EMAIL_TO_ACCOUNT = "linfang@didihu.com.cn";
public final static String EMAIL_TO_ACCOUNT = "494097103@qq.com";
public final static String SPLIT_CHAR = ";";
// smtp.xxx(domainName).com
// public final static String EMAIL_FROM_HOST =
// PropertiesUtil.getProperty("car.ocr.result.email.from.host");
// public final static int EMAIL_FROM_PORT = Integer
// .parseInt(PropertiesUtil.getProperty("car.ocr.result.email.from.port"));
// public final static String EMAIL_FROM_ACCOUNT =
// PropertiesUtil.getProperty("car.ocr.result.email.from.account");
// public final static String EMAIL_FROM_PASSWORD =
// PropertiesUtil.getProperty("car.ocr.result.email.from.password");
// // public final static String EMAIL_TO_ACCOUNT = "linfang@didihu.com.cn";
// public final static String EMAIL_TO_ACCOUNT =
// PropertiesUtil.getProperty("car.ocr.result.email.to");
public static StringBuilder EMAILCONTENT = new StringBuilder(
"<table style=\"font-size:12px;\" width=\"97%\" border=\"1\" cellspacing=\"0\" cellpadding=\"2\" bordercolor=\"#009900\">"
+ "<tr><td width=\"11%\">订单编号</td><td width=\"10%\">订单信息</td><td width=\"8%\">识别结果</td>"
+ "<td width=\"8%\">不通过原因</td><td >全车外观图</td><td width=\"8%\">识别结果</td><td width=\"8%\">不通过原因</td><td>车架号及设备SN合照</td></tr>");
public static void sendEmail(String address, String title, String content) throws Exception {
Transport transport = null;
if (null != address && address.trim().length() > 0) {
try {
MimeMessage message = new MimeMessage(EMAIL_SESSION);
message.setFrom(new InternetAddress(EMAIL_FROM_ACCOUNT));
for (String addr : address.trim().split(SPLIT_CHAR)) {
if (null != addr && addr.trim().length() > 0)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(addr.trim()));
}
message.setSubject(title);
Multipart part = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(content, "text/html; charset=UTF-8");
part.addBodyPart(contentPart);
message.setContent(part);
transport = EMAIL_SESSION.getTransport("smtp");
transport.connect(EMAIL_FROM_HOST, EMAIL_FROM_PORT, EMAIL_FROM_ACCOUNT, EMAIL_FROM_PASSWORD);
message.saveChanges();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
} finally {
close(transport);
}
}
}
public static void close(Transport s) { // transport, store
if (null != s) {
try {
s.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
EMAILCONTENT
.append("<tr><td >81282020032024</td><td >有线+无线设备<br>安装单</td><td>不通过</td>"
+ "<td >车牌号模糊</td><td >https://bhg.didihu.com.cn/group1/M00/07/39/wKgDPV5zIH2AYSF0AADiR2I_x7U420.jpg</td><td >不通过</td><td >不通过原因是XXX</td><td>https://bhg.didihu.com.cn/group1/M00/07/39/wKgDPV5zIH2AYSF0AADiR2I_x7U420.jpg</td></tr></table>");
EmilUtils.sendEmail(EMAIL_TO_ACCOUNT, "2020年3月23日图像识别情况汇总", EMAILCONTENT.toString());
}
}