使用SpringBoot实现邮件发送(QQ邮箱为例)

使用SpringBoot实现邮件发送(QQ邮箱为例)

一、获取授权码

1.首先进入qq邮箱找到设置

2、账号栏目,找到POP3/SMTP服务 并开启服务

3、获取授权码

二、SpringBoot集成邮件发送

1.创建邮件发送服务类

java 复制代码
package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSendException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    // 发送简单的文本邮件
    public void sendSimpleEmail(String to, String subject, String text) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("your-email@qq.com");  // 你的 QQ 邮件地址
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            mailSender.send(message);
            System.out.println("邮件发送成功");
        } catch (MailSendException e) {
            System.out.println("发送失败: " + e.getMessage());
        }
    }

    // 发送带有 HTML 内容的邮件
    public void sendHtmlEmail(String to, String subject, String htmlContent) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("your-email@qq.com");  // 你的 QQ 邮件地址
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(htmlContent, true);  // true 表示 HTML 格式
            mailSender.send(message);
            System.out.println("HTML 邮件发送成功");
        } catch (MailException | MessagingException e) {
            System.out.println("发送失败: " + e.getMessage());
        }
    }
}

2.创建邮件发送控制器

java 复制代码
package com.example.demo.controller;

import com.example.demo.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private EmailService emailService;

    // 发送简单邮件
    @GetMapping("/sendTextEmail")
    public String sendTextEmail() {
        emailService.sendSimpleEmail("recipient@example.com", "测试邮件", "这是一个测试邮件!");
        return "邮件已发送";
    }

    // 发送 HTML 邮件
    @GetMapping("/sendHtmlEmail")
    public String sendHtmlEmail() {
        String htmlContent = "<h1>欢迎</h1><p>这是一个 <b>HTML</b> 格式的邮件。</p>";
        emailService.sendHtmlEmail("recipient@example.com", "HTML 邮件", htmlContent);
        return "HTML 邮件已发送";
    }
}

3.application.yml 配置:

方案 1:使用端口 465(SSL):
yaml 复制代码
spring:
    mail:
      host: smtp.qq.com
      username: XXXXXXXXXXX@qq.com
      password: xxxxxxxxxxxxx  # QQ邮箱的授权码
      port: 465  # SSL端口
      default-encoding: UTF-8
      properties:
        mail:
          smtp:
            ssl:
              enable: true  # 启用SSL加密
              required: true  # 确保使用SSL
            debug: true  # 启用调试模式,便于排查错误
方案 2:使用端口 587(STARTTLS):
yaml 复制代码
spring:
	mail:
      host: smtp.qq.com
      username: XXXXXXXXXXX@qq.com
      password: xxxxxxxxxxxx  # QQ邮箱的授权码
      port: 587  # STARTTLS端口
      default-encoding: UTF-8
      properties:
        mail:
          smtp:
            starttls:
              enable: true  # 启用STARTTLS加密
            debug: true  # 启用调试模式,便于排查错误

三、测试邮件发送功能

启动 Spring Boot 项目后,访问以下 URL 来测试邮件发送功能

发送简单文本邮件:

bash 复制代码
http://localhost:8080/email/sendTextEmail

发送 HTML 邮件:

bash 复制代码
http://localhost:8080/email/sendHtmlEmail

如果配置正确,浏览器将显示 邮件已发送HTML 邮件已发送,并且收件人会收到相应的邮件。

如果配置正确,浏览器将显示 邮件已发送HTML 邮件已发送,并且收件人会收到相应的邮件。

OVER!

相关推荐
章豪Mrrey nical3 小时前
前后端分离工作详解Detailed Explanation of Frontend-Backend Separation Work
后端·前端框架·状态模式
超级大只老咪4 小时前
数组相邻元素比较的循环条件(Java竞赛考点)
java
小浣熊熊熊熊熊熊熊丶4 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
毕设源码-钟学长4 小时前
【开题答辩全过程】以 公交管理系统为例,包含答辩的问题和答案
java·eclipse
啃火龙果的兔子4 小时前
JDK 安装配置
java·开发语言
星哥说事4 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
派大鑫wink4 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
程序员爱钓鱼5 小时前
Node.js 编程实战:文件读写操作
前端·后端·node.js
xUxIAOrUIII5 小时前
【Spring Boot】控制器Controller方法
java·spring boot·后端
Dolphin_Home5 小时前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法