使用 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

相关推荐
m0_748238637 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
Forget the Dream28 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek31 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
清河__1 小时前
【Go】十七、grpc 服务的具体功能编写
开发语言·后端·golang
Forget the Dream1 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
张声录11 小时前
国密算法Sm2工具类--golang实现版
开发语言·后端·golang
帅的飞起来1 小时前
设计模式--spring中用到的设计模式
java·spring·设计模式