使用 Spring Boot 实现邮件发送功能

推荐一个AI网站,免费使用豆包AI模型,快去白嫖👉海鲸AI

SpringBoot------发送邮件

在现代应用程序中,发送邮件是一个常见的需求。本文将介绍如何使用 Spring Boot 发送邮件。我们将从新建一个 Spring Boot 项目开始,逐步配置项目,并编写代码实现邮件发送功能。

新建一个 Spring Boot 项目

首先,我们需要创建一个新的 Spring Boot 项目。你可以使用 Spring Initializr 或者你的 IDE 来创建项目。确保选择以下依赖项:

  • Spring Web
  • Spring Boot DevTools
  • Spring Boot Starter Mail

pom.xml

在创建好项目后,打开 pom.xml 文件,确保包含以下依赖项:

xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

application.properties

接下来,我们需要在 application.properties 文件中配置邮件服务器的相关信息。以下是一个示例配置:

properties 复制代码
spring.mail.host=smtp.example.com
spring.mail.port=587
spring.mail.username=your-email@example.com
spring.mail.password=your-email-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

请将 smtp.example.comyour-email@example.comyour-email-password 替换为你实际使用的邮件服务器地址、邮箱地址和密码。

SpringEmailService

现在,我们创建一个服务类 SpringEmailService 来处理邮件发送的逻辑。

java 复制代码
package com.example.springemail;

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 SpringEmailService {

    @Autowired
    private JavaMailSender mailSender;

    public void sendSimpleEmail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        message.setFrom("your-email@example.com");

        mailSender.send(message);
    }
}

在这个类中,我们使用 JavaMailSender 来发送简单的文本邮件。sendSimpleEmail 方法接受收件人地址、邮件主题和邮件内容作为参数。

SpringbootJavaMailApplication

接下来,我们在 SpringbootJavaMailApplication 类中编写一个简单的控制器,来触发邮件发送。

java 复制代码
package com.example.springemail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootJavaMailApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootJavaMailApplication.class, args);
    }
}

@RestController
class EmailController {

    @Autowired
    private SpringEmailService emailService;

    @GetMapping("/sendEmail")
    public String sendEmail(@RequestParam String to, @RequestParam String subject, @RequestParam String text) {
        emailService.sendSimpleEmail(to, subject, text);
        return "Email sent successfully";
    }
}

在这个示例中,我们创建了一个简单的 REST 控制器 EmailController,它包含一个 /sendEmail 端点。通过访问这个端点并传递必要的参数,我们可以发送邮件。

项目总结

在本文中,我们介绍了如何使用 Spring Boot 发送邮件。我们从新建项目开始,配置了 pom.xmlapplication.properties 文件,编写了 SpringEmailService 服务类,并在 SpringbootJavaMailApplication 中创建了一个简单的控制器来触发邮件发送。

通过这些步骤,你可以轻松地在 Spring Boot 项目中实现邮件发送功能。希望这篇文章对你有所帮助!

推荐一个AI网站,免费使用豆包AI模型,快去白嫖👉海鲸AI

相关推荐
知其然亦知其所以然6 分钟前
JVM社招面试题:队列和栈是什么?有什么区别?我在面试现场讲了个故事…
java·后端·面试
武子康10 分钟前
大数据-30 ZooKeeper Java-API 监听节点 创建、删除节点
大数据·后端·zookeeper
知了一笑10 分钟前
SpringBoot3集成多款主流大模型
spring boot·后端·openai
wmze12 分钟前
InnoDB存储引擎--索引与锁
后端
harmful_sheep13 分钟前
Spring 为何需要三级缓存解决循环依赖,而不是二级缓存
java·spring·缓存
星辰大海的精灵15 分钟前
如何确保全球数据管道中的跨时区数据完整性和一致性
java·后端·架构
调试人生的显微镜17 分钟前
iOS App首次启动请求异常调试:一次冷启动链路抓包与初始化流程修复
后端
大大。18 分钟前
van-tabbar-item选中active数据变了,图标没变
java·服务器·前端
AI小智20 分钟前
Context Engineering:AI 工程的下一个前沿阵地?
后端
paopaokaka_luck20 分钟前
基于SpringBoot+Vue的酒类仓储管理系统
数据库·vue.js·spring boot·后端·小程序