SpringBoot发送邮件功能

1.获取授权码

设置——账户——POP3/SMTP服务——开启

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.SpringBoot配置

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

application.yml

spring:
  mail:
    host: smtp.qq.com
    username: xxxxxxxx@qq.com
    password: xxxxxxxxxxxxxxx

在这里插入图片描述

3.基础文字发送(可用作验证码)

代码

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import javax.annotation.Resource;

@SpringBootTest
class DemoApplicationTests {
    @Resource
    private JavaMailSenderImpl javaMailSender;
    @Test
    void contextLoads() {
        SimpleMailMessage msg = new SimpleMailMessage();
        // 内容
        msg.setText("验证码:8828(10分钟内有效)");
        // 主题
        msg.setSubject("xx龙小店注册验证码");
        // 收件人邮箱
        msg.setTo("xxxxxxxxx@163.com");
        // 发送人邮箱
        msg.setFrom("xxxxxxxxxxx@qq.com");

        javaMailSender.send(msg);
    }
}

效果图
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring Boot 提供了简单而强大的邮件发送功能。以下是使用 Spring Boot 发送邮件的基本步骤: 1. 配置邮件发送参数:在 `application.properties`(或 `application.yml`)文件中配置邮件发送相关的参数,例如邮件服务器地址、端口、认证信息等。例如: ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring[email protected] spring.mail.password=your-email-password ``` 2. 创建一个邮件发送服务类:创建一个 Java 类,用于封装邮件发送的逻辑。可以使用 Spring Boot 提供的 `JavaMailSender` 类来发送邮件。例如: ```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 EmailService { @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); } } ``` 3. 在需要发送邮件的地方调用邮件发送服务:在需要发送邮件的地方,注入邮件发送服务,并调用其方法发送邮件。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class EmailController { @Autowired private EmailService emailService; @GetMapping("/send-email") public String sendEmail() { emailService.sendEmail("[email protected]", "Hello", "This is a test email"); return "Email sent"; } } ``` 以上是使用 Spring Boot 发送邮件的基本步骤。根据实际需求,你还可以使用模板引擎来发送 HTML 邮件,添加附件等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qiweilong123456

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

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

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

打赏作者

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

抵扣说明:

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

余额充值