使用Springboot发送邮件(QQ邮箱)整合笔记
一、邮件需求
当我们登陆、注册、绑定等使用网站的时候,很多时候需要结合自己的邮箱来绑定保护自己帐号的安全;而注册激活账号一般有两种形式,一种是手机短信验证码激活,另一种是邮箱激活。今天主要讲述使用邮件
二、整合开发
一、创建Springboot项目hello-rabbit,核心依赖如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--spring boot web的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mail 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--thymeleaf模版依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--freemarker模版依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
二、获取QQ邮箱授权码(QQ邮箱-设置-账户-POP3/SMTP服务开启,发送短信即可获取,记得保存好)
三、配置application.yml文件,如图
spring:
mail:
host: smtp.qq.com
username: 发件的邮箱
password: 发件邮箱的授权码
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
四、新建包email,并创建类EmailConfig.java
EmailConfig.java
package com.basic.hellorabbit.email;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Description: 配置发送邮件
*/
@Data
@Component
public class EmailConfig {
/**
* 发件人
*/
@Value("${spring.mail.username}")
private String emailFrom;
}
五、新建包名 service和impl,并创建EmailService.java和EmailServiceImpl.java(分别使用了freemarker和thymeleaf两种模版,测试分别放开进行)
1.EmailService.java
package com.basic.hellorabbit.service;
import java.io.File;
public interface EmailService {
/**
* 发送文本/简单的邮件
* @param receiverName 接收人
* @param title 标题
* @param content 内容
*/
void sendStringEmail(String receiverName, String title, String content);
/**
* 发送大文件/附件的邮件
* @param receiverName
* @param title
* @param content
* @param file 文件
*/
void sendBigEmail(String receiverName, String title, String content, File file);
/**
* 发送模版邮件
* @param receiverName
* @param title
* @param information 模版参数名(html页面)
*/
void sendTemplateEmail(String receiverName, String title, String information);
}
2.EmailServiceImpl.java
package com.basic.hellorabbit.service.impl;
import com.basic.hellorabbit.email.EmailConfig;
import com.basic.hellorabbit.service.EmailService;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
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.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@Service
public class EmailServiceImpl implements EmailService {
@Autowired
private EmailConfig emailConfig;
@Autowired
private JavaMailSender mailSender;
@Autowired
private TemplateEngine templateEngine;
@Autowired
private FreeMarkerConfigurer markerConfigurer;
//发送文本/简单的邮件
@Override
public void sendStringEmail(String receiverName, String title, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(emailConfig.getEmailFrom());
message.setTo(receiverName);
message.setSubject(title);
message.setText(content);
mailSender.send(message);
}
//发送大文件/附件的邮件
@Override
public void sendBigEmail(String receiverName, String title, String content, File file) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(emailConfig.getEmailFrom());
helper.setTo(receiverName);
helper.setSubject(title);
helper.setText(content);
FileSystemResource resource = new FileSystemResource(file);
helper.addAttachment("附件", resource);
}catch (Exception e){
e.printStackTrace();
}
mailSender.send(message);
}
//发送模版邮件
@Override
public void sendTemplateEmail(String receiverName, String title, String information) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(emailConfig.getEmailFrom());
helper.setTo(receiverName);
helper.setSubject(title);
//封装模版使用的数据
Map<String,Object> map = new HashMap<>();
map.put("username","明世隐");
//1.FreeMarker
//1-1 获取FreeMarker模版
//Template markertemplate = markerConfigurer.getConfiguration().getTemplate(information);
//1-2 将模版内容转为字符串类型并将参数传入
//String markertTtml = FreeMarkerTemplateUtils.processTemplateIntoString(markertemplate, map);
//1-3 将字符串作为邮件内容
//helper.setText(markertTtml,true);
//2.Thymeleaf
//2-1 获取Thymeleaf模版
Context context = new Context();
context.setVariable("username","瑶");
//2-2 将模版内容转为字符串类型并将参数传入
String thymeleafHtml = templateEngine.process("thymeleafTemplate", context);
helper.setText(thymeleafHtml,true);
}catch (Exception e){
e.printStackTrace();
}
mailSender.send(message);
}
}
*六、在*resources/templates下新建freemarkerTemplate.html和thymeleafTemplate.html**用来做文件模版
1.freemarkerTemplate.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>freemarkerTemplate</title>
</head>
<body>
${username}你好!<br>感谢您的使用,这是你的激活邮件,请点击下面的链接进行激活。<br>
</body>
</html>
2.thymeleafTemplate.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>thymeleafTemplate</title>
</head>
<body>
<span th:text="${username}"></span>,你好,感谢您的使用,这是你的激活邮件,请点击下面的链接进行激活。<br>
</body>
</html>
*七、新建类 *EmailController.java**进行测试(代码中mmmmmm@qq.com为收件人邮箱)
EmailController.java
package com.basic.hellorabbit.controller;
import com.basic.hellorabbit.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
/**
* @Description: 邮件
*/
@Controller
public class EmailController {
@Autowired
private EmailService emailService;
@ResponseBody
@RequestMapping(value = "/stringEmaile",method = RequestMethod.GET)
public String sendStringEmail(){
emailService.sendStringEmail("mmmmmm@qq.com","你好","我发的你收到了吗?");
return "发送成功";
}
@ResponseBody
@RequestMapping(value = "/bigEmaile",method = RequestMethod.GET)
public String sendBigEmail(){
File file = new File("src/main/resources/static/zong.jpg");
emailService.sendBigEmail("mmmmmm@qq.com","你好,美图请查收","看我拍的咋样?",file);
return "发送成功";
}
@ResponseBody
@RequestMapping(value = "/templateEmail",method = RequestMethod.GET)
public String sendTemplateEmail(){
//emailService.sendTemplateEmail("mmmmmm@qq.com","激活邮件","freemarkerTemplate.html");
emailService.sendTemplateEmail("mmmmmm@qq.com","激活邮件","thymeleafTemplate.html");
return "发送成功";
}
}