使用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("[email protected]");  // 你的 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("[email protected]");  // 你的 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("[email protected]", "测试邮件", "这是一个测试邮件!");
        return "邮件已发送";
    }

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

3.application.yml 配置:

方案 1:使用端口 465(SSL):
yaml 复制代码
spring:
    mail:
      host: smtp.qq.com
      username: [email protected]
      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: [email protected]
      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!

相关推荐
一个public的class20 分钟前
什么是 Java 泛型
java·开发语言·后端
士别三日&&当刮目相看23 分钟前
JAVA学习*Object类
java·开发语言·学习
清风絮柳24 分钟前
51. “闲转易”交易平台小程序(基于springboot&vue)
vue.js·spring boot·小程序·毕业设计·校园二手交易平台·二手交易小程序·闲转易交易系统
快来卷java43 分钟前
MySQL篇(一):慢查询定位及索引、B树相关知识详解
java·数据结构·b树·mysql·adb
凸头1 小时前
I/O多路复用 + Reactor和Proactor + 一致性哈希
java·哈希算法
头孢头孢1 小时前
k8s常用总结
运维·后端·k8s
TheITSea2 小时前
后端开发 SpringBoot 工程模板
spring boot·后端
Asthenia04122 小时前
编译原理中的词法分析器:从文本到符号的桥梁
后端
慵懒学者2 小时前
15 网络编程:三要素(IP地址、端口、协议)、UDP通信实现和TCP通信实现 (黑马Java视频笔记)
java·网络·笔记·tcp/ip·udp
anda01092 小时前
11-leveldb compact原理和性能优化
java·开发语言·性能优化