SpringBoot整合spring-boot-starter-mail 发送 QQ邮件

SpringBoot整合spring-boot-starter-mail 发送 QQ邮件

1、设置QQ邮箱,查看流程:QQ邮箱——>设置——>账户——>开启POP3/SMTP服务,生成授权码:邮件开启xxxxxxxxx
在这里插入图片描述
在这里插入图片描述
2、导入mail依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-study</artifactId>
        <groupId>org.bc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>e-mail</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
        <dependency>
            <groupId>org.bc</groupId>
            <artifactId>microboot-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>

3、配置yaml相关信息

server:
  port: 80
spring:
  mail:
    host: smtp.qq.com # 一定要使用合法的SMTP
    username: 111111@qq.com # 用户名
    password: xxxxxxx # 是临时生成的密码
    properties:
      mail.smtp.auth: true # 启用SMTP认证
      mail.smtp.starttls.enabled: true # 启用SMTP认证
      mail.smtp.starttls.required: true # 必须采用加密链接

4、编写发送邮件模块

package com.yootk.test;

import com.yootk.StartSpringBootApplication;
import com.yootk.service.IMessageService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;

@ExtendWith(SpringExtension.class) // 使用JUnit5测试工具
@WebAppConfiguration // 启动WEB运行环境
@SpringBootTest(classes = StartSpringBootApplication.class) // 配置程序启动类
public class TestSendEmail { // 编写测试类
    @Autowired
    private JavaMailSender javaMailSender; // 发送邮件工具类
    @Test
    public void testSend() {    // 进行响应测试
        SimpleMailMessage message = new SimpleMailMessage(); // 建立一个简单的邮件结构
        message.setFrom("1111@qq.com");
        message.setTo("181211111@qq.com"); // 邮件的接收者
        message.setSubject("来老师给你的祝福。");
        message.setText("wwwww");
        this.javaMailSender.send(message); // 邮件发送
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然可以!下面是一个简单的示例代码,演示了如何使用Spring Boot Starter Mail发送电子邮件: 首先,确保在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 然后,在application.properties文件中配置邮件服务器的相关信息: ```properties # 邮件服务器主机名 spring.mail.host=your-mail-server-hostname # 邮件服务器端口号 spring.mail.port=your-mail-server-port # 邮件服务器用户名 spring.mail.username=your-username # 邮件服务器密码 spring.mail.password=your-password # 邮件传输协议 spring.mail.protocol=smtp # 是否启用调试模式 spring.mail.properties.mail.debug=true ``` 接下来,创建一个邮件发送服务类,例如MailService.java: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class MailService { @Autowired private JavaMailSender mailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); mailSender.send(message); } } ``` 最后,在需要发送邮件的地方调用MailService的sendEmail方法即可: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApplication { @Autowired private MailService mailService; public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } public void sendEmail() { String to = "recipient@example.com"; String subject = "Hello"; String text = "This is a test email."; mailService.sendEmail(to, subject, text); } } ``` 这是一个简单的示例代码,演示了如何使用Spring Boot Starter Mail发送电子邮件。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码农-004

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

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

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

打赏作者

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

抵扣说明:

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

余额充值