SpringBoot发送邮件

本文介绍了如何使用SpringBoot发送邮件,从配置pom.xml、设置邮箱属性,到编写测试类实现简单邮件发送,再到利用Thymeleaf美化邮件内容,通过引入HTML模板提升邮件的视觉效果。

最近把算打注册邮箱验证弄一下

学习了下如何用SpringBoot发送邮件

首先我们要在SpringBoot里配置pom.xml文件


    
1
2
3
4

    
<dependency>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-mail </artifactId>
</dependency>

这个就是spring推出的JavaMailSender
比起之前的JavaMail 更加简化了邮件发送的过程

在application.properties中添加邮箱配置


    
1
2
3
4
5
6

    
spring.mail.host=smtp.mxhichina.com
spring.mail.username=邮箱账号
spring.mail.password=密码
spring.mail.default-encoding=UTF-8
mail.fromMail.addr=你想要用来发送的邮箱

第一个是smtp服务(详细Google下,确保你的邮箱开启了smtp)

编写一个简单的类来测试

为了防止初学者导错包,我把import也写进来


    
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

    
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
* Created by cw on 2017/7/18.
*/
@Component
public class MailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private JavaMailSender mailSender;
@Value("${mail.fromMail.addr}")
private String from;
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
logger.info("简单邮件已经发送。");
} catch (Exception e) {
logger.error("发送简单邮件时发生异常!", e);
}
}
}

这个就是来负责发送邮件的类了

我们编写测试类来测试下


    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

    
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.thymeleaf.TemplateEngine;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
private MailService mailService;
@Autowired
private TemplateEngine templateEngine;
@Test
public void testSimpleMail() throws Exception {
mailService.sendSimpleMail("123456789@qq.com","测试邮件"," 你好");
}
}

邮件运行测试类
如果按照上面的步骤来的话,123456789@qq.com里应该会收到一份邮件(请用自己的邮箱来测试)

但是这样的邮件是不是太单调了?
那么我们可以html来美化邮件

用thymeleaf来美化邮件

在pom.xml文件里引入


    
1
2
3
4

    
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在MailService里添加新的方法来发送html邮件


    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

    
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//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);
logger.info("html邮件发送成功");
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常!", e);
}
}

在resources文件夹中的templates里新建HTML文件


    
1
2
3
4
5
6
7
8
9
10
11

    
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
您好,这是验证邮件,请点击下面的链接完成验证,<br/>
<a href="#" th:href="@{ http://www.google.com/{id}(id=${id}) }">激活账号</a>
</body>
</html>

测试一下


    
1
2
3
4
5
6
7
8
9
10
11

    
import org.thymeleaf.context.Context;
@Test
public void sendTemplateMail() {
//创建邮件正文
Context context = new Context();
context.setVariable("id", "1");
String emailContent = templateEngine.process("emailTemplate", context);
mailService.sendHtmlMail("123456789@qq.com","主题:这是模板邮件",emailContent);
}

这里我们传入了一个id 到html里

运行测试类,你的邮箱应该又收到了一封邮件
不过这封会更加美观?

这个就是SpringBoot发送邮件的简单教程了

本篇参考了这篇博客
也可以去这里学习下

评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值