Spring Boot 集成 JavaMail 163邮箱配置详解

Spring Boot 集成 JavaMail 发送邮件 - 163邮箱配置详解

在实际项目开发中,邮件发送是一个常见的功能需求,比如用户注册验证码、订单通知、密码重置等场景。Spring Boot 提供了 spring-boot-starter-mail 来简化邮件发送的开发。本文将详细介绍如何在 Spring Boot 中配置 163 邮箱发送邮件。

环境准备

  • Spring Boot 2.x 或 3.x
  • Java 8+
  • 163 邮箱账号(需要开启 SMTP 服务)

1. 添加 Maven 依赖

pom.xml 中添加邮件 starter 依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2. 配置 163 邮箱

2.1 开启 SMTP 服务

登录 163 邮箱,进入「设置」→「POP3/SMTP/IMAP」,开启 SMTP 服务

注意:163 邮箱需要设置授权码,而非登录密码。

2.2 配置文件

application.ymlapplication.properties 中配置:

application.yml 配置:

yaml 复制代码
spring:
  mail:
    host: smtp.163.com
    port: 465
    username: your-email@163.com
    password: your-authorization-code
    protocol: smtp
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
          ssl:
            enable: true
            trust: smtp.163.com

application.properties 配置:

properties 复制代码
spring.mail.host=smtp.163.com
spring.mail.port=465
spring.mail.username=your-email@163.com
spring.mail.password=your-authorization-code
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.ssl.trust=smtp.163.com

2.3 配置说明

配置项 说明
host 163 邮箱 SMTP 服务器地址
port 465(SSL 端口)或 587(TLS 端口)
username 你的 163 邮箱地址
password 163 邮箱授权码(不是登录密码)
protocol 邮件协议,通常为 smtp

3. 注入 JavaMailSender

Spring Boot 会自动配置 JavaMailSender,直接注入使用即可:

java 复制代码
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MailService {

    @Autowired
    private JavaMailSender mailSender;

    public void sendSimpleMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("your-email@163.com");  // 发件人
        message.setTo("recipient@example.com"); // 收件人
        message.setSubject("测试邮件");          // 主题
        message.setText("这是一封测试邮件");       // 内容
        
        mailSender.send(message);
    }
}

4. 发送 HTML 邮件

SimpleMailMessage 只支持纯文本,如果需要发送 HTML 格式的邮件,使用 MimeMessageHelper

java 复制代码
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

public void sendHtmlMail() throws MessagingException {
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
    
    helper.setFrom("your-email@163.com");
    helper.setTo("recipient@example.com");
    helper.setSubject("HTML 邮件测试");
    
    // 设置 HTML 内容
    String htmlContent = "<h1>标题</h1><p>这是一封 <b>HTML</b> 邮件</p>";
    helper.setText(htmlContent, true);
    
    mailSender.send(mimeMessage);
}

5. 发送带附件的邮件

java 复制代码
public void sendAttachmentMail() throws MessagingException, IOException {
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
    
    helper.setFrom("your-email@163.com");
    helper.setTo("recipient@example.com");
    helper.setSubject("带附件的邮件");
    helper.setText("请查收附件");
    
    // 添加附件
    File file = new File("path/to/file.pdf");
    helper.addAttachment("file.pdf", file);
    
    mailSender.send(mimeMessage);
}

6. 常见问题排查

6.1 认证失败

  • 检查是否使用了正确的授权码(不是登录密码)
  • 确认 SMTP 服务已在邮箱设置中开启

6.2 连接超时

  • 检查防火墙是否阻止了 SMTP 端口
  • 确认网络能否访问 smtp.163.com

6.3 SSL 证书问题

  • 确保配置了正确的 SSL 参数
  • 163 邮箱推荐使用 465 端口(SSL)或 587 端口(TLS)

7. 完整示例

创建一个简单的 Spring Boot 项目,完整配置如下:

java 复制代码
// MailService.java
@Service
@Slf4j
public class MailService {

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

    @Autowired
    private JavaMailSender mailSender;

    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        
        try {
            mailSender.send(message);
            log.info("邮件发送成功: {}", to);
        } catch (Exception e) {
            log.error("邮件发送失败: {}", e.getMessage());
        }
    }
}

总结

本文详细介绍了 Spring Boot 集成 JavaMail 配置 163 邮箱的完整步骤,包括:

  1. 添加 Maven 依赖
  2. 配置 163 邮箱 SMTP 参数
  3. 发送简单邮件、HTML 邮件和带附件的邮件
  4. 常见问题排查

通过以上配置,即可在 Spring Boot 项目中轻松实现邮件发送功能。

相关推荐
咖啡八杯13 分钟前
GoF设计模式——工厂方法模式
java·后端·设计模式
勿忘初心122114 分钟前
SpringBoot 国密 SM4 配置加密(自动解密处理器实现)
spring boot·国密 sm4·自动解密处理器
代码羊羊1 小时前
Rust 迭代器完全通俗易懂指南(零基础全覆盖)
java·开发语言·rust
MY_TEUCK8 小时前
【Java 后端】SpringBoot 登录认证与会话跟踪实战(JWT + Filter/Interceptor)
java·开发语言·spring boot
今天长肉了吗8 小时前
银行风控项目踩坑实录:指标跑了6小时,风险评分全挂了
java
计算机程序定制辅导8 小时前
计算机小程序毕设实战-基于Spring Boot与微信小程序的考研资源共享平台设计与实现基于springboot+微信小程序的考研复习辅助平台【完整源码+LW+部署说明+演示视频,全bao一条龙等】
spring boot·微信小程序·小程序·课程设计
随读手机9 小时前
多式联运信息交互平台完整方案(2026版)
java·ai·eclipse·云计算·区块链
许彰午9 小时前
03-二叉树——从递归遍历到非递归实现
java·算法
nj012810 小时前
Spring 循环依赖详解:三级缓存、早期引用、AOP 代理与懒加载
java·spring·缓存
野生技术架构师10 小时前
2026年最全Java面试题及答案汇总(建议收藏,面试前看这篇就够了)
java·开发语言·面试