这是很久之前的代码了,最近需要使用了,了就记录一下。
maven:
1
2
3
4
5
|
<
dependency
>
<
groupId
>com.sun.mail</
groupId
>
<
artifactId
>javax.mail</
artifactId
>
<
version
>1.5.5</
version
>
</
dependency
>
|
spring配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!-- 邮件 -->
<
bean
id
=
"baojingMailSender"
class
=
"org.springframework.mail.javamail.JavaMailSenderImpl"
>
<!-- 服务器 -->
<
property
name
=
"host"
value
=
"#{configproperties['baojing.mail.host']}"
/>
<
property
name
=
"protocol"
value
=
"#{configproperties['baojing.mail.protocol']}"
></
property
>
<!-- 端口号 -->
<
property
name
=
"port"
value
=
"#{configproperties['baojing.mail.port']}"
/>
<!-- 用户名 -->
<!-- <property name="username" value="#{configproperties['baojing.mail.username']}" /> -->
<!-- 密码 -->
<!-- <property name="password" value="#{configproperties['baojing.mail.password']}" /> -->
<!-- SMTP服务器验证 -->
<
property
name
=
"javaMailProperties"
>
<
props
>
<!-- 验证身份 -->
<
prop
key
=
"mail.smtp.auth"
>{configproperties['baojing.mail.smtp.auth']}</
prop
>
<
prop
key
=
"mail.smtp.timeout"
>#{configproperties['baojing.mail.smtp.timeout']}</
prop
>
</
props
>
</
property
>
</
bean
>
|
配置:
1
2
3
4
5
6
7
|
baojing.mail.host = mail2-
in
.XXXX.com
baojing.mail.port = 25
baojing.mail.protocol = smtp
baojing.mail.username =
""
baojing.mail.password =
""
baojing.mail.smtp.auth =
false
baojing.mail.smtp.timeout = 25000
|
代码:
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
|
@Autowired
@Qualifier
(value=
"baojingMailSender"
)
private
JavaMailSender baojingMailSender;
/**
* 发邮件
* **/
@SuppressWarnings
(
"static-access"
)
public
void
sendMail(
final
Map<String, Map<String, String>> message){
Map<String, String> dayDiffMap = message.get(
"day"
);
Map<String, String> hourDiffMap = message.get(
"hour"
);
Map<String, String> defaultMap =
new
HashMap<String, String>();
defaultMap.put(
"ratio"
,
"0"
);
defaultMap.put(
"diffCount"
,
"0"
);
defaultMap.put(
"totleCount"
,
"0"
);
defaultMap.put(
"todayFile"
,
"0"
);
defaultMap.put(
"diffFile"
,
"0"
);
//避免空指向
if
(dayDiffMap ==
null
|| dayDiffMap.isEmpty()) {
dayDiffMap = defaultMap;
}
if
(hourDiffMap ==
null
|| hourDiffMap.isEmpty() ) {
hourDiffMap = defaultMap;
}
if
(VoiceConstans.isSendMail && (Double.valueOf(dayDiffMap.get(
"ratio"
)) >= VoiceConstans.dayThreshold ||
Double.valueOf(hourDiffMap.get(
"ratio"
)) >= VoiceConstans.hourThreshold)) {
String dayRatio =
new
String().format(
"%.2f"
, (Double.valueOf(dayDiffMap.get(
"ratio"
)) *
100
)) +
"%"
;
String hourRatio =
new
String().format(
"%.2f"
, (Double.valueOf(hourDiffMap.get(
"ratio"
)) *
100
)) +
"%"
;
try
{
//HTML
MimeMessage mimeMessage = baojingMailSender.createMimeMessage();
MimeMessageHelper messageHelper =
new
MimeMessageHelper(mimeMessage,
true
,
"utf-8"
);
String [] mailto =
new
String[VoiceConstans.mailTo.size()];
messageHelper.setTo(VoiceConstans.mailTo.toArray(mailto));
messageHelper.setFrom(VoiceConstans.mailFrom);
messageHelper.setSubject(VoiceConstans.mailSubject+
" ("
+DateUtil.format(
new
Date(), VoiceConstans.HOURFORMAT)+
")"
);
StringBuffer htmlBuffer =
new
StringBuffer();
htmlBuffer.append(
"HI,ALL:<br>"
);
htmlBuffer.append(DateUtil.format(
new
Date(),
"yyyy-MM-dd HH时"
));
htmlBuffer.append(
"数据如下:<br>"
);
htmlBuffer.append(
"总数:"
+dayDiffMap.get(
"totleCount"
)+
"<br>"
);
htmlBuffer.append(
"<table border=\"1\"><thead><tr><th>同日DIFF数</th><th>同日比</th><th>相邻DIFF数</th><th>相邻比</th></thead><tbody><tr>"
);
htmlBuffer.append(
"<td>"
+dayDiffMap.get(
"diffCount"
)+
"</td>"
);
//同日当前diff数
htmlBuffer.append(
"<td>"
+dayRatio+
"</td>"
);
//同日比
htmlBuffer.append(
"<td>"
+hourDiffMap.get(
"diffCount"
)+
"</td>"
);
//相邻当前diff数
htmlBuffer.append(
"<td>"
+hourRatio+
"</td>"
);
//相邻比
htmlBuffer.append(
"</tr></tbody></table><br><br>"
);
LOGGER.info(
"发邮件: "
+ htmlBuffer.toString());
messageHelper.setText(htmlBuffer.toString(),
true
);
baojingMailSender.send(mimeMessage);
}
catch
(MessagingException e) {
e.printStackTrace();
}
}
}
|
版权声明:原创作品,如需转载,请注明出处。否则将追究法律责任
本文转自 梦朝思夕 51CTO博客,原文链接:http://blog.51cto.com/qiangmzsx/1859044