SpringBoot邮件服务spring-boot-starter-mail

SpringBoot邮件服务

Spring 框架通过使用 JavaMailSender接口,Spring Boot 为其提供了自动配置以及启动模块

官方地址

点击跳转:https://docs.spring.io/spring-boot/docs/current/reference/html/io.html#io.email

实例

准备工作

开通SMTP服务(QQ)

进入账号昵称管理
开启第一个服务
在这里插入图片描述

在这里插入图片描述
如下获取到16位授权码
在这里插入图片描述

1.引入依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>2.7.3</version>
</dependency>

或者你可以选择创建spring boot项目时进行选择
在这里插入图片描述

2.编写yaml配置(QQ邮箱)

spring:
  mail:
    default-encoding: UTF-8
    host: smtp.qq.com
    port: 587
    username: 你的qq邮箱地址xxx@qq.com
    password: 16位授权码,请参考准备工作

3.简单文本文件发送测试案例

@SpringBootTest
class Test1ApplicationTests {
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Test
    void contextLoads() {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("你的qq邮箱地址xxx@qq.com");
        mailMessage.setTo("发送的目标地址");
        mailMessage.setText("你好 hello world");
        mailMessage.setSubject("测试Spring邮箱服务");
        mailSender.send(mailMessage);
        System.out.println("====完成发送!====");
    }

}

结果:
在这里插入图片描述

官方文档案例

https://docs.spring.io/spring-framework/docs/5.3.22/reference/html/integration.html#mail-usage-simple

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值