添加依赖: spring-boot-starter-mail

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> 复制代码

2 邮件配置

在application.yml配置文件中添加邮件干系配置(以163邮箱为例)

html5发邮件应用Spring Boot 发送邮件 SQL

spring: ## 邮件配置 mail: host: smtp.163.com username: 邮箱 password: 邮箱授权密码 properties: mail: smtp: auth: true starttls: enable: true required: true复制代码3 发送大略邮件

利用Spring Boot发送大略邮件

@Service(value = "jmsService")public class JmsService { @Autowired private JavaMailSender jms; public void sendSimpleMail(String sender, String receiver, String subject, String text) { try { SimpleMailmessage message = new SimpleMailMessage(); message.setFrom(sender); // 发送者 message.setTo(receiver); // 接管者 message.setSubject(subject); // 邮件主题 message.setText(text); // 邮件正文 jms.send(message); } catch (Exception exception) { // TODO 非常处理 } }}复制代码4 发送HTML格式邮件

利用Spring Boot发送HTML格式邮件

@Service(value = "jmsService")public class JmsService { @Autowired private JavaMailSender jms; public void sendHtmlMail(String sender, String receiver, String subject, String htmlText) { MimeMessage message = null; try { message = jms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); // 发送者 helper.setTo(receiver); // 接管者 helper.setSubject(subject); // 邮件主题 if (StringUtil.isNotEmpty(htmlText)) { StringBuffer sb = new StringBuffer(htmlText); helper.setText(sb.toString(), true); // HTML } jms.send(message); } catch (Exception exception) { // TODO 非常处理 } }}复制代码5 发送带附件的邮件

利用Spring Boot发送带附件的邮件

@Service(value = "jmsService")public class JmsService { @Autowired private JavaMailSender jms; public void sendSimpleMailWithAttachment(String sender, String receiver, String subject, String filePath) { MimeMessage message = null; try { message = jms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); // 发送者 helper.setTo(receiver); // 接管者 helper.setSubject(subject); // 邮件主题 helper.setText("详情拜会附件内容!
"); // 邮件正文 File file = new File(filePath); FileSystemResource fileResource = new FileSystemResource(file); helper.addAttachment(file.getName(), fileResource); // 附件 jms.send(message); } catch (Exception exception) { // TODO 非常处理 } }}复制代码
6 发送模板邮件

利用Spring Boot发送模板邮件,以thymeleaf为例

6.1 引入模板引擎

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 复制代码

6.2 thymeleaf配置

spring: ## thymelea模板配置 thymeleaf: #thymeleaf 所在路径 prefix: classpath:/templates/ #thymeleaf 后缀 suffix: .html #thymeleaf 采取的标准 mode: HTML5 #thymeleaf 编码格式 encoding: UTF-8复制代码6.3 新建邮件模板

在项目resources/templates目录下新建邮件模板template.html

<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>注册码</title></head><body> 您好,您的注册码为<span th:text="${code}"></span>,请在5分钟内利用完成操作。
</body></html>复制代码
6.4 发送模板邮件

@Service(value = "jmsService")public class JmsService { @Autowired private JavaMailSender jms; @Autowired private TemplateEngine templateEngine; public void sendTemplateEmail(String sender, String receiver, String subject, String code) { MimeMessage message = null; try { message = jms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); // 发送者 helper.setTo(receiver); // 接管者 helper.setSubject(subject); // 邮件主题 Context context = new Context(); context.setVariable("code", code); // 模板邮件参数 String template = templateEngine.process("template", context); helper.setText(template, true); jms.send(message); } catch (Exception exception) { // TODO 非常处理 } }}

链接:https://juejin.im/post/6876818580286603278来源:掘金