sillyby

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,页面代码乖乖到页面文件里面,需要时直接调取该页面文件,整合模板引擎就是一个不错的解决方案。

分类:

技术点:

相关文章: