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

相关推荐
Tang102421 分钟前
Glide 4.x 版本的图片加载流程
java
振宇i23 分钟前
Java 富文本转word
java·word·富文本·docx4j
李菠菜23 分钟前
Java对象映射利器MapStruct应用详解与实战指南
java
神仙别闹26 分钟前
基于Java+MySQL 实现(Web)日程管理系统
java·前端·mysql
执念36526 分钟前
MySQL基础
后端
黯_森27 分钟前
Java异常机制
java·后端
A阳俊yi1 小时前
Spring Boot日志配置
java·spring boot·后端
苹果酱05671 小时前
2020-06-23 暑期学习日更计划(机器学习入门之路(资源汇总)+概率论)
java·vue.js·spring boot·mysql·课程设计
起风了布布1 小时前
配置版本化是怎么实现的
后端
资深前端外卖员1 小时前
【nodejs高可用】前端APM应用监控方案 + 落地
前端·后端