使用 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
[email protected]
spring.mail.password=your-email-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

请将 smtp.example.com[email protected]your-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("[email protected]");

        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

相关推荐
有什么东东14 分钟前
山东大学软件学院创新项目实训开发日志(9)之测试前后端连接
java
zhangpeng45554794016 分钟前
用Java写一个MVCC例子
java·开发语言
续亮~24 分钟前
ANP协议深度解析:智能体网络协议的演进与革新
网络·后端·网络协议·ai·ai编程
谦行28 分钟前
前端视角 Java Web 入门手册 5.1:真实世界 Web 开发——初识 Spring Boot
java·后端
自在如风。33 分钟前
Java 设计模式:策略模式详解
java·设计模式·策略模式
!!!52535 分钟前
Spring Boot 整合 MongoDB:分页查询详解 (新手友好)
spring boot·后端·mongodb
普通网友38 分钟前
如何在CentOS部署青龙面板并实现无公网IP远程访问本地面板
开发语言·后端·golang
小杨40443 分钟前
springboot框架项目实践应用十八(nacos高级特性)
spring boot·后端·spring cloud
API小爬虫44 分钟前
如何利用 Java 爬虫获取京东商品详情信息
java·开发语言·爬虫
菜鸟起航ing1 小时前
【Java面试系列】Spring Boot微服务架构下的分布式事务解决方案与性能优化详解 - 3-5年Java开发必备知识
java·spring boot·微服务·性能优化·分布式事务