MailServiceImpl.java
4.29 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
package com.huaheng.pc.config.sendMail.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* Created by Enzo Cotter on 2019/9/6.
*/
@Service
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
@Value("${spring.mail.username}")
private String from;
@Resource
private JavaMailSender mailSender;
@Override
public Boolean sendSimpleMail(String[] to, String subject, String content, String[] cc) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);//收信人
message.setSubject(subject);//主题
message.setText(content);//内容
message.setFrom(from);//发信人
if (cc.length != 0){
message.setCc(cc);
}
try {
mailSender.send(message);
return true;
} catch (MailException me){
return false;
}
}
@Override
public Boolean sendHtmlMail(String[] to, String subject, String content, String[] cc) {
logger.info("发送HTML邮件开始:{},{},{}", to, subject, content);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);//true代表支持html
if (cc.length != 0){
helper.setCc(cc);
}
mailSender.send(message);
logger.info("发送HTML邮件成功");
return true;
} catch (MessagingException e) {
logger.error("发送HTML邮件失败:", e);
return false;
}
}
@Override
public Boolean sendAttachmentMail(String[] to, String subject, String content, String filePath, String[] cc) {
logger.info("发送带附件邮件开始:{},{},{},{}", to, subject, content, filePath);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);//添加附件,可多次调用该方法添加多个附件
mailSender.send(message);
logger.info("发送带附件邮件成功");
return true;
} catch (MessagingException e) {
logger.error("发送带附件邮件失败", e);
return false;
}
}
@Override
public Boolean sendInlineResourceMail(String[] to, String subject, String content, String rscPath, String rscId, String[] cc) {
logger.info("发送带图片邮件开始:{},{},{},{},{},{}", to, subject, content, rscPath, rscId, cc);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);//重复使用添加多个图片
mailSender.send(message);
logger.info("发送带图片邮件成功");
return true;
} catch (MessagingException e) {
logger.error("发送带图片邮件失败", e);
return false;
}
}
}