使用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!

相关推荐
巫山老妖2 小时前
从零开发一个掘金自动发布 Skill,并上架 Clawhub
后端
颜酱2 小时前
图的数据结构:从「多叉树」到存储与遍历
javascript·后端·算法
雨中飘荡的记忆3 小时前
零拷贝技术深度解析
后端
uzong4 小时前
十年老员工的项目管理实战心得:有道有术
后端
Victor3565 小时前
MongoDB(31)索引对查询性能有何影响?
后端
Victor3565 小时前
MongoDB(30)如何删除索引?
后端
lizhongxuan5 小时前
多 Agent 协同机制对比
后端
IT_陈寒6 小时前
SpringBoot项目启动慢?5个技巧让你的应用秒级响应!
前端·人工智能·后端
树上有只程序猿6 小时前
2026低代码选型指南,主流低代码开发平台排名出炉
前端·后端
高端章鱼哥7 小时前
为什么说用OpenClaw对打工人来说“不划算”
前端·后端