Spring Boot整合邮件配置

Spring Boot整合邮件配置

概述

这个技术是做什么?学习该技术的原因,技术的难点在哪里。

这个技术能使项目具备发送邮件的功能,这个技术我是作为技术储备来学习的,没想到在学习后没多久就能够有用武之地。该项技术总体难度不大,硬要说难的地方就在于整合模板引擎发送模板邮件,因为还要同时了解一些模板引擎的知识,不过如果有JSP相关知识会容易应付得多。

整合邮件发送功能

Spring Boot 2.x集成了mail模块

image-20200614220248741

在pom.xml中引入依赖

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

邮箱配置

一些必要的名词解释

  • 什么是POP3、SMTP和IMAP?
    他们是三种邮件协议。简单来说,POP3和IMAP是用来从服务器上下载邮件的。SMTP适用于发送或中转信件时找到下一个目的地。所以我们发送邮件应该使用SMTP协议。

  • 什么是邮箱客户端授权码?
    邮箱客户端授权码是为了避免邮箱密码被盗后,盗号者通过客户端登录邮箱而设计的安防功能。

QQ邮箱配置

网页登录QQ邮箱→设置→开启相应服务并生成授权码

image-20200614211908944

spring boot配置:

spring:
  mail:
    host: smtp.qq.com #发送邮件服务器
    username: xx@qq.com #QQ邮箱
    password: xxxxxxxxxxx #客户端授权码
    protocol: smtp #发送邮件协议
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 465 #端口号465或587
    properties.mail.display.sendmail: aaa #可以任意
    properties.mail.display.sendname: bbb #可以任意
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true #开启SSL
    default-encoding: utf-8

网易系(126/163/yeah)邮箱配置

网页登录网易邮箱→设置→POP3/SMTP/IMAP

image-20200614212409282

spring boot配置:

spring:
  mail:
    host: smtp.126.com #发送邮件服务器
    username: xx@126.com #网易邮箱
    password: xxxxxxxx #客户端授权码
    protocol: smtp #发送邮件协议
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 994 #465或者994
    properties.mail.display.sendmail: aaa #可以任意
    properties.mail.display.sendname: bbb #可以任意
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true #开启SSL
    default-encoding: utf-8
    from: xx@126.com
  • 126邮箱SMTP服务器地址:smtp.126.com
  • 163邮箱SMTP服务器地址:smtp.163.com
  • yeah邮箱SMTP服务器地址:smtp.yeah.net

发送简单的文本邮件

写个邮件服务Service

@Service
public class MailService {
    // Spring官方提供的集成邮件服务的实现类,目前是Java后端发送邮件和集成邮件服务的主流工具。
    @Resource
    private JavaMailSender mailSender;

    // 从配置文件中注入发件人的姓名
    @Value("${spring.mail.username}")
    private String fromEmail;

    /**
     * 发送文本邮件
     *
     * @param to      收件人
     * @param subject 标题
     * @param content 正文
     * @throws MessagingException
     */
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail); // 发件人
        message.setTo(to);	
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }
}

在业务中调用该服务的方法即可

mailService.sendSimpleMail("xxxxxx@xx.com","普通文本邮件","普通文本邮件内容");

发送html邮件

为了方便,在原来的Service里直接添加一个方法

/**
 * 发送html邮件
 */
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
    //注意这里使用的是MimeMessage
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(to);
    helper.setSubject(subject);
    //第二个参数:格式是否为html
    helper.setText(content, true);

    mailSender.send(message);
}

调用该方法,直接传入html代码的字符串作为正文参数:

mailService.sendHtmlMail("xxxxxx@xx.com","一封html测试邮件","
			"<div style=\"text-align: center;position: absolute;\" >\n"
            +"<h3>\"一封html测试邮件\"</h3>\n"
            + "<div>一封html测试邮件</div>\n"
            + "</div>");

像上面直接传递html字符串发送html邮件,在java类里写html代码总有点怪怪的,而且有很明显的缺点,若是要用相同样式发送不同的内容,代码冗余度就会增加;而且若是需要发送一个复杂的html页面,代码看起来就一团乱麻,而且不方便调整邮件的样式。

我们希望html和java分离开,在java里就只管java,页面代码乖乖到页面文件里面,需要时直接调取该页面文件,整合模板引擎就是一个不错的解决方案。👇

发送基于模板的邮件(以模板引擎freemarker为例)

该方法本质上还是发送html邮件,只不过是有一个把模板转换成html字符串的过程,thymeleaf也可以实现。这个方法还能使你的系统发出的邮件更加美观。

image-20200614213422214

说明:这里不详细介绍freemarker的内容,在这里只描述它的一个使用场景——生成电子邮件,想要进一步了解freemarker请行学习

添加依赖:

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

springboot配置

spring:
  freemarker:
    cache: false # 缓存配置 开发阶段应该配置为false 因为经常会改
    suffix: .html # 模版后缀名 默认为ftl
    charset: UTF-8 # 文件编码
    template-loader-path: classpath:/templates/  # 存放模板的文件夹,以resource文件夹为相对路径

在存放模板的文件夹下写一个html模板

image-20200614170329668

内容如下:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>freemarker简单示例</title>
</head>
<body>
<h1>Hello Freemarker</h1>
    <div>My name is ${myname}</div>
</body>
</html>

仍然为了方便,在原来的Service里直接添加代码

@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;

@Test
public void sendTemplateMail(String to, String subject, String template) throws IOException, TemplateException, MessagingException {
    // 获得模板
    Template template = freeMarkerConfigurer.getConfiguration().getTemplate(template);
	// 使用Map作为数据模型,定义属性和值
    Map<String,Object> model = new HashMap<>();
    model.put("myname","ZYF");
    // 传入数据模型到模板,替代模板中的占位符,并将模板转化为html字符串
    String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template,model);
	// 该方法本质上还是发送html邮件,调用之前发送html邮件的方法
    this.sendHtmlMail(to, subject, templateHtml);
}

要用的时候调用即可

mailService.sendTemplateMail("xxxxx@xx.com", "基于模板的html邮件", "fremarkertemp.html");

发送带附件的邮件

话不多说上代码:

/**
 * 发送带附件的邮件
 */
public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException {
    MimeMessage message = mailSender.createMimeMessage();
    //要带附件第二个参数设为true
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(fromEmail);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(content, true);

    FileSystemResource file = new FileSystemResource(new File(filePath));
    String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
    helper.addAttachment(fileName, file);

    mailSender.send(message);
}

调用时传入文件路径:

String filePath = "D:\\projects\\springboot\\template.png";
mailService.sendAttachmentsMail("xxxx@xx.com", "带附件的邮件", "邮件中有附件", filePath);

邮箱服务实战

这是我某次实践中邮箱服务的运用,目的是为了能够定时发送邮件,由于发送邮件是耗时操作,为了不妨碍系统处理用户请求,添加了@Async注解,定时任务将会在独立的线程中被执行,下面放上链接:

ScheduleJob.java

MailServiceImpl.java

EmailTemplate.html

application.yml

我的踩坑记录

邮件服务器连接失败

org.springframework.mail.MailSendException: Mail server connection failed; 
...
nested exception is:
  java.net.UnknownHostException: smtp.163.com 
 ...
  1. 网络问题

    控制台输入ping smtp.163.com看看是否能ping通;

  2. 配置问题

    检查一下application.yml的邮件服务器配置有没有拼写或格式错误(比如多按了一个空格);

    开启SSL时使用587端口时是无法连接QQ邮件服务器的,请使用465端口

授权失败

org.springframework.mail.MailAuthenticationException: Authentication failed;nested exception is
javax.mail.AuthenticationFailedException: 550 User has no permission
...

这个坑是我在教同学时遇到的

按照上文去打开邮箱的stmp服务即可解决

消息发送失败

org.springframework.mail.MailSendException: Failed messages: 
 
com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163 smtp11,D8CowACX7CmSHB5b3SrlCA--.26635S3 
 
1528700051,please see http://mail.163.com/help/help_spam_16.htm?ip=182.138.102.204&hostid=smtp11&time=1528700051
...

点进报错信息提供的网址瞧瞧,是网易官方的退信代码说明,也就是说我们发送的消息被退回来了:

image-20200614214050517

注意到报错信息的退信代码554,看看554是啥来头:

image-20200614214201544

原来是被识别为垃圾邮件了,检查一下邮件的主题及内容,使用合法信息,我当时是因为邮件内容包含test测试这些字眼所以被拦截了。

总结

spring boot整合邮件服务并不难,就是踩到坑的时候挺烦的,但这也是学习新知识所必须经历的。

参考链接

Spring Boot的特性:发送邮件,Arvin Chen

springboot使用freemarker发送模板邮件,Mr_Yao,CSDN

企业退信的常见问题?-163邮箱常见问题

posted @ 2020-06-15 15:48  YF_Zhang  阅读(8322)  评论(3编辑  收藏  举报