【Java】使用 Spring Boot 集成 QQ 邮箱并发送邮件

目录

1、配置邮件服务

参数解释:

2、获取QQ邮箱授权码

3、引入依赖

4、创建控制器

5、接口测试


        在现代应用程序开发中,发送邮件是一个常见的需求。Spring Boot 提供了强大的支持,使得集成邮件服务变得非常简单。本文将介绍如何使用 Spring Boot 集成 QQ 邮箱,并通过 QQ 邮箱发送邮件,此文也是便于自己以后进行参考。

1、配置邮件服务

application.properties 文件中,添加以下配置:

# ===========邮箱相关=========
spring.mail.host=smtp.qq.com
spring.mail.username= 123456@qq.com
spring.mail.password= 你qq邮箱申请的授权码
spring.mail.port=587
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.socketFactoryClass=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true

或在 application.yml文件中,添加以下配置:

spring:
#  ===========邮箱相关=========
  mail:
    host: smtp.qq.com
    username: 123456@qq.com
    password: xxxxx 
    port: 587
    default-encoding: UTF-8
    properties:
      mail:
        smtp:
          socketFactoryClass: javax.net.ssl.SSLSocketFactory
        debug: true
参数解释:

    spring.mail.host:配置 SMTP 服务器地址。
    spring.mail.username:配置发送者邮箱。
    spring.mail.password:配置密码,注意这里填写的是qq邮箱申请的授权码,而不是真正的邮箱密码。
    spring.mail.port:配置端口号,可以是 465 或 587。
    spring.mail.default-encoding:配置默认的邮件编码为 UTF-8。
    spring.mail.properties.mail.smtp.socketFactoryClass:配置 SSL 加密工厂。
    spring.mail.properties.mail.debug:表示开启 DEBUG 模式,这样,邮件发送过程的日志会在控制台打印出来,方便排查错误。

2、获取QQ邮箱授权码

通过以下步骤获取授权码:

2.1进入qq邮箱首页,点击设置-账号。

2.2找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 点击管理服务,按照操作进行发送短信登验证。

2.3查看是否开启,点击生成授权码,发送短信进行后续操作。

2.4验证成功,生成授权码

上述就完成了最重要的步骤获取到了授权码,下面就通过代码进行测试了。

3、引入依赖

<!--qq邮箱-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

4、创建控制器

创建一个名为 EmailController 的控制器,用于处理邮件发送请求:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

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

    @Autowired
    private JavaMailSender javaMailSender;

    //读取yml文件中username的值并赋值给form
    @Value("${spring.mail.username}")
    private String from;

    @RequestMapping("sendEmail")
    public String sendSimpleMail(@RequestParam(value = "emailReceiver") String emailReceiver) {
            // 构建一个邮件对象
            SimpleMailMessage message = new SimpleMailMessage();
            // 设置邮件发送者
            message.setFrom(from);
            // 设置邮件接收者
            message.setTo(emailReceiver);
            // 设置邮件的主题
            message.setSubject("登录验证码");
            // 设置邮件的正文
            Random random = new Random();
            StringBuilder code = new StringBuilder();
            for (int i = 0; i < 6; i++) {
                int r = random.nextInt(10);
                code.append(r);
            }
            String text = "您的验证码为:" + code + ",请勿泄露给他人。";
            message.setText(text);
            // 发送邮件
        try {
            javaMailSender.send(message);
            return "发送成功";
        } catch (MailException e) {
            e.printStackTrace();
        }
        return "发送失败";
    }
}

5、接口测试

127.0.0.1:9899/email/sendEmail?emailReceiver=123456789@qq.com

 

验证成功。

  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot可以使用JavaMailSender来发送邮件。下面是使用QQ邮箱发送邮件的步骤: 1. 在QQ邮箱中开启SMTP服务,并生成授权码。 2. 在pom.xml中添加依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 3. 在application.properties中配置邮箱信息: ```properties spring.mail.host=smtp.qq.com spring.mail.port=587 spring.mail.username=你的QQ邮箱 spring.mail.password=你的授权码 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 4. 创建一个邮件服务类: ```java @Service public class MailService { @Autowired private JavaMailSender mailSender; public void sendMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("你的QQ邮箱"); message.setTo(to); message.setSubject(subject); message.setText(content); mailSender.send(message); } } ``` 5. 在需要发送邮件的地方调用MailService的sendMail方法即可。 ```java @Autowired private MailService mailService; mailService.sendMail("收件人邮箱", "邮件主题", "邮件内容"); ``` 注意事项: 1. 需要在QQ邮箱中开启SMTP服务,并生成授权码。 2. 如果使用SSL,需要将port改为465,并配置spring.mail.properties.mail.smtp.ssl.enable=true。 3. 邮箱的用户名和密码需要进行加密处理,可以使用spring-boot-starter-security依赖来加密。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mxin5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值