环境: Spring Boot 2.1.5.RELEASE 、Java 8
构建环境
在pom.xml
文件中加入依赖
<!-- Thymeleaf 模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 发送邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
在application.properties
文件中加入163邮箱的配置
## 发送邮件配置
spring.mail.host=smtp.163.com # 163邮箱的smtp
spring.mail.port=25 # 端口,
spring.mail.username=邮箱的用户名
spring.mail.password=邮箱的授权码(不是登录密码)
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
163邮箱的收取邮件支持POP/IMAP两种协议,发送邮件采用SMTP协议,收件和发件均使用SSL协议来进行加密传输,采用SSL协议需要单独对帐户进行设置。采用SSL协议和非SSL协议时端口号有所区别,参照下表的一些常见配置组合:
类型 | 服务器名称 | 服务器地址 | SSL协议端口号 | 非SSL协议端口号 |
---|---|---|---|---|
收件服务器 | POP | pop.163.com | 995 | 110 |
收件服务器 | IMAP | imap.163.com | 993 | 143 |
发件服务器 | SMTP | smtp.163.com | 465/994 | 25 |
163邮箱的地址和端口: http://help.163.com/10/0731/11/6CTUBPT300753VB8.html
发送邮件的工具类
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.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Map;
@Component
public class EmailUtil {
private static final Logger logger = LoggerFactory.getLogger(EmailUtil.class);
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private TemplateEngine templateEngine;
@Value("${spring.mail.username}")
private String senderMailAddress;
public void sendSimpleMail(Map<String, Object> valueMap){
MimeMessage mimeMessage = null;
try {
mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// 设置发件人邮箱
helper.setFrom(senderMailAddress);
// 设置收件人邮箱
helper.setTo((String[])valueMap.get("to"));
// 设置邮件标题
helper.setSubject(valueMap.get("title").toString());
// 设置邮件正文
helper.setSubject(valueMap.get("content").toString());
// 添加正文(使用thymeleaf模板)
Context context = new Context();
context.setVariables(valueMap);
String content = this.templateEngine.process("mail", context);
helper.setText(content, true);
// 发送邮件
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
logger.error("发送邮件抛出了异常,信息为:"+ e.getCause());
}
}
}
HTML模板mail.html
代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>邮箱发送</title>
</head>
<body>
<p><span th:text="${title}"></span></p>
<p th:text="${content}"></p>
</body>
</html>
MailController.java
类中的代码
import com.tengxt.demo.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;
import java.util.Map;
@Controller
public class MailController {
@Autowired
private EmailUtil emailUtil;
@RequestMapping("/mail")
public void sendMail(){
Map<String, Object> valueMap = new HashMap<>();
valueMap.put("to", new String[]{"收件人邮箱1","收件人邮箱2",...);
valueMap.put("title", "测试邮件标题");
valueMap.put("content", "测试邮件内容");
// 调用发送邮件的方法
emailUtil.sendSimpleMail(valueMap);
}
}