Skip to content

准备工作

修改 163 邮箱的 POP3 配置,备份授权码。详情请参考:传送门

添加依赖

在 pom.xml 文件中加入下面依赖(如果有就不用加了)

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

配置授权码

在 application.properties 文件中配置授权码

properties
spring.mail.host=smtp.163.com
spring.mail.username=邮箱
spring.mail.password=授权码
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8

封装工具类 Mail.java

java
package com.api.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class Mail {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 简单文本邮件
     * @param to 接收者邮件
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String to, String subject, String content){

        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        message.setFrom(from);

        mailSender.send(message);
    }

    /**
     * HTML 文本邮件
     * @param to 接收者邮件
     * @param subject 邮件主题
     * @param content HTML内容
     * @throws MessagingException
     */
    public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        helper.setFrom(from);

        mailSender.send(message);
    }

}

在 Controller 中使用工具类

注意代码第 17、33 行

java
package com.api.controller;

import com.api.utils.Mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mail")
public class MailController {

    @Autowired
    private Mail mail;

    @RequestMapping("/sendSimpleMail")
    public Map<String,Object> sendSimpleMail() {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        mail.sendSimpleMail("sevenone_m@163.com","测试标题","测试内容");

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    @RequestMapping("/sendHtmlMail")
    public Map<String,Object> sendHtmlMail() throws MessagingException {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        mail.sendHtmlMail("sevenone_m@163.com","hello world邮件","<html><body>\n" +
                "<h3>Hello !</h3>\n" +
                "<h4>欢迎注册 xxx 系统,验证码为:<u>354216</u></h4>" +
                "<body></html>");

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

}

参考文献

Spring boot 项目(十五)——实现163邮箱发送邮件

基于 MIT 许可发布