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

相关推荐
兆子龙31 分钟前
ahooks useMemoizedFn:解决 useCallback 的依赖地狱
java·javascript
ん贤3 小时前
Go channel 深入解析
开发语言·后端·golang
曹牧5 小时前
BeanUtils.copyProperties‌
java
changhong19866 小时前
如何在 Spring Boot 中配置数据库?
数据库·spring boot·后端
QWQ___qwq6 小时前
Java线程安全深度总结:基本类型与引用类型的本质区别
java·安全·面试
识君啊6 小时前
Java异常处理:中小厂面试通关指南
java·开发语言·面试·异常处理·exception·中小厂
月月玩代码8 小时前
Actuator,Spring Boot应用监控与管理端点!
java·spring boot·后端
XPoet9 小时前
AI 编程工程化:Skill——给你的 AI 员工装上技能包
前端·后端·ai编程
阿珍爱上了阿强,在一个有星星的夜晚9 小时前
node后端页面性能监测分析
java·学习方法
Java程序之猿9 小时前
SpringBoot + camel+IBM MQ实现消息队列处理
java·spring boot·mybatis