spring boot 发送邮件

这篇文章主要讲解 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
114
package 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;
@Slf4j
@Component
@ConfigurationProperties(prefix = "mail")
public class MailUtil {
@Autowired
private JavaMailSender sender;
@Getter @Setter
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
2
3
4
5
6
7
8
9
spring.mail.host=smtp-mail.outlook.com
spring.mail.port=587
spring.mail.username=你邮箱的用户名
spring.mail.password=你邮箱的密码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.timeout=180000
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
mail.from=你邮箱的用户名

这里我用的是outlook

host 根据不同邮件服务商进行修改

我们来看看测试类

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
package org.xxz.util;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.xxz.blog.util.MailUtil;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailUtilTest {
@Autowired
MailUtil mailUtil;
String to = "修改成要接受邮件的邮箱地址";
@Test
public void testSendText() throws Exception {
mailUtil.sendText(to, "测试text", "测试text");
}
@Test
public void testSendHtml() throws Exception {
mailUtil.sendHtml(to, "测试html", "<h1>测试html</h1>");
}
@Test
public void testSendAttachments() throws Exception {
mailUtil.sendAttachments(to, "测试attachments", "测试attachments", "d:\\attachments.txt");
}
@Test
public void testSendInlineResource() throws Exception {
// 注意图片的src=cid:xxx 与后面的要对应
mailUtil.sendInlineResource(to, "测试inlineResource", "测试inline resource<img src=\"cid:001\">", "d:\\ihehe.jpg", "001");
}
}