SpringBoot版本:2.0.3
pom包配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置文件
163郵箱配置
spring.mail.host=smtp.163.com #郵箱服務器地址
spring.mail.username=xxx.163.com #用戶名
spring.mail.password=ooo #開啓POP3之後設置的客戶端授權碼
spring.mail.default-encoding=UTF-8 #編碼
# 超時時間(可選)
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
126郵箱配置
spring.mail.host=smtp.126.com
spring.mail.username=xxx.126.com
spring.mail.password=ooo #開啓POP3之後設置的客戶端授權碼
spring.mail.default-encoding=UTF-8
qq郵箱配置
spring.mail.host=smtp.qq.com
[email protected]
spring.mail.password=ooo #開啓POP3之後設置的客戶端授權碼
spring.mail.default-encoding=UTF-8
1、這裏的password不是登錄密碼,是開啓POP3之後設置的客戶端授權碼
2、 默認端口25,使用465端口時,需要添加配置:
spring.mail.port=465
spring.mail.properties.mail.smtp.ssl.enable=true
JavaMailSender
Spring已經幫我們內置了JavaMailSender,可以直接在項目中引用
簡單的文本郵件
/**
* MailService實現類
*/
@Component
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Override
public void sendSimpleMail(String to, String subject, String content) throws MailException {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from); // 郵件發送者
message.setTo(to); // 郵件接受者
message.setSubject(subject); // 主題
message.setText(content); // 內容
mailSender.send(message);
}
}
富文本郵件
發送富文本郵件需要使用MimeMessageHelper類,MimeMessageHelper支持發送複雜郵件模板,支持文本、附件、HTML、圖片等。
發送帶圖片的郵件
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
File file = new File(rscPath);
FileSystemResource res = new FileSystemResource(file);
helper.addInline(rscId, res);
mailSender.send(message);
}
如果需要發送多張圖片,可以改變傳參方式,使用集合添加多個
<img src='cid:rscId'>
和
helper.addInline(rscId, res);
即可實現
單元測試:
@Test
public void test2() {
String to = "[email protected]";
String subject = "今晚要加班,不用等我了";
String rscId = "img110";
String content = "<html><body><img width='250px' src=\'cid:" + rscId + "\'></body></html>";
// 此處爲linux系統路徑
String imgPath = "/Users/kx/WechatIMG16.jpeg";
try {
mailService.sendInlineResourceMail(to, subject, content, imgPath, rscId);
System.out.println("成功了");
} catch (MessagingException e) {
System.out.println("失敗了");
e.printStackTrace();
}
}
結果:
這裏使用的qq郵箱作爲收件人,結果被坑慘了。剛開始收到的郵件都是破圖,查看源碼發現src也沒有沒有圖片地址,回去看了一波代碼,好久之後終於發現破圖的原因了:qq郵箱默認把我的圖片屏蔽掉了,然後在收件人下方、正文上方有一行黃色的警告,點擊信任此郵箱,我的天,終於看見圖片了。
發送HTML郵件
@Override
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
//true 表⽰示需要創建⼀一個 multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
}
單元測試:
@Test
public void test() {
String to = "[email protected]";
String subject = "猜猜我今天買了啥";
String content = "<html><head></head><body><h3>哈哈,什麼都沒有</h3></body></html>";
try {
mailService.sendHtmlMail(to, subject, content);
System.out.println("成功了");
} catch (MessagingException e) {
System.out.println("失敗了");
e.printStackTrace();
}
}
結果:
發送帶附件的郵件
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper 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);
}
如果有多個附件,同樣可以改變傳參方式,使用集合多次調用
helper.addAttachment(fileName, file);
單元測試:
@Test
public void test3() {
String to = "[email protected]";
String subject = "這是一個有附件的郵件,記得接受文件";
String content = "嗯哼?自己看附件";
String imgPath = "/Users/kx/WechatIMG16.jpeg";
try {
mailService.sendAttachmentsMail(to, subject, content, imgPath);
System.out.println("成功了");
} catch (MessagingException e) {
System.out.println("失敗了");
e.printStackTrace();
}
}
結果:
值得一提的是:之前在找圖片是破圖的時候,發現如果有圖片但是沒有對應的
<img/>
,接收到的郵件就會以附件的方式存在
發送失敗
郵件發送有着很多的原因,如:配置過程中的端口號/授權碼錯誤、550 DT:SPM主題/內容不規範被當做垃圾郵件、發送過於頻繁、網絡原因。