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

相关推荐
leobertlan6 小时前
2025年终总结
前端·后端·程序员
面向Google编程6 小时前
从零学习Kafka:数据存储
后端·kafka
易安说AI7 小时前
Claude Opus 4.6 凌晨发布,我体验了一整晚,说说真实感受。
后端
易安说AI7 小时前
Ralph Loop 让Claude无止尽干活的牛马...
前端·后端
易安说AI7 小时前
用 Claude Code 远程分析生产日志,追踪 Claude Max 账户被封原因
后端
JH30738 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
颜酱9 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
Coder_Boy_9 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
invicinble9 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟9 小时前
使用ASM和agent监控属性变化
java