这篇文章主要讲解 spring boot如何发送邮件
发送邮件是一个很常用的功能,比如线上故障告警,验证码等功能都会用到,下面我们来看看用spring mail 来实现发送邮件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
114package org.xxz.util;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
4j
"mail") (prefix =
public class MailUtil {
private JavaMailSender sender;
private String from;
/**
* 发送文本邮件
* @param to 接受人
* @param subject 主题
* @param text 文本
*/
public void sendText(String to, String subject, String text) throws MessagingException {
log.info("===>to:{}, subject:{}, text:{}", to, subject, text);
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(text);
sender.send(message);
log.info("===>send text mail finish");
}
/**
* 发送html邮件
* @param to
* @param subject
* @param text
* @throws MessagingException
*/
public void sendHtml(String to, String subject, String text) throws MessagingException {
log.info("===>to:{}, subject:{}, text:{}", to, subject, text);
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText(text, true);
sender.send(message);
log.info("===>send html mail finish");
}
/**
* 发送附件邮件
* @param to
* @param subject
* @param text
* @param filePath
* @throws MessagingException
*/
public void sendAttachments(String to, String subject, String text, String filePath) throws MessagingException {
log.info("===>to:{}, subject:{}, text:{}, filePath:{}", to, subject, text, filePath);
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText(text, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String attachmentFilename = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
helper.addAttachment(attachmentFilename, file);
sender.send(message);
log.info("===>send attachments mail finish");
}
/**
* 发送嵌入资源(一般是图片)邮件
* @param to
* @param subject
* @param text 带图片的邮件<img src=\"cid:resId1\">
* @param resPath 文件路径
* @param resId 静态资源id
* @throws MessagingException
*/
public void sendInlineResource(String to, String subject, String text, String resPath, String resId) throws MessagingException {
log.info("===>to:{}, subject:{}, text:{}, resPath:{}, resId:{}", to, subject, text, resPath, resId);
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText(text, true);
FileSystemResource resource = new FileSystemResource(new File(resPath));
helper.addInline(resId, resource);
sender.send(message);
log.info("===>send inline resource mail finish");
}
}
我们在看看配置文件怎么配置(application.properties)
1 | spring.mail.host=smtp-mail.outlook.com |
这里我用的是outlook
host 根据不同邮件服务商进行修改
我们来看看测试类
1 | package org.xxz.util; |
本文标题:spring boot 发送邮件
本文链接:https://xxzkid.github.io/2017/spring-boot-send-mail/