【java深入学习第7章】用 Spring Boot 和 Java Mail 轻松实现邮件发送功能
创始人
2024-12-26 04:10:38
0

引言

在现代的企业应用中,邮件发送是一个非常常见的功能。无论是用户注册后的验证邮件,还是系统通知邮件,邮件服务都扮演着重要的角色。本文将介绍如何在Spring Boot项目中整合Java Mail,实现发送邮件的功能。

一、准备工作

在开始之前,我们需要准备以下内容:

  • 一个Spring Boot项目
  • 一个可用的SMTP邮件服务器(如Gmail、QQ邮箱等)

二、添加依赖

首先,在pom.xml中添加Spring Boot Starter Mail依赖:

              org.springframework.boot         spring-boot-starter-mail       

三、配置邮件服务器

application.propertiesapplication.yml中配置邮件服务器信息。以下是使用Gmail SMTP服务器的示例配置:

application.properties

spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=your-email@gmail.com spring.mail.password=your-email-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true 

application.yml

spring:   mail:     host: smtp.gmail.com     port: 587     username: your-email@gmail.com     password: your-email-password     properties:       mail:         smtp:           auth: true           starttls:             enable: true 

四、编写邮件发送服务

创建一个MailService类,用于封装邮件发送的逻辑:

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 sendSimpleMail(String to, String subject, String content) {         SimpleMailMessage message = new SimpleMailMessage();         message.setFrom("your-email@gmail.com");         message.setTo(to);         message.setSubject(subject);         message.setText(content);         mailSender.send(message);     } } 

五、编写控制器

创建一个控制器MailController,提供一个发送邮件的接口:

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;  @RestController public class MailController {      @Autowired     private MailService mailService;      @GetMapping("/sendMail")     public String sendMail(@RequestParam String to, @RequestParam String subject, @RequestParam String content) {         mailService.sendSimpleMail(to, subject, content);         return "Mail sent successfully";     } } 

六、测试邮件发送功能

启动Spring Boot应用,访问以下URL测试邮件发送功能:

http://localhost:8080/sendMail?to=recipient-email@gmail.com&subject=Test&content=This is a test email. 

如果配置正确并且邮件服务器可用,你应该会收到一封测试邮件。

七、发送HTML邮件

除了发送简单文本邮件,Java Mail还支持发送HTML格式的邮件。我们可以在MailService中添加一个方法来发送HTML邮件:

import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage;  public void sendHtmlMail(String to, String subject, String content) throws MessagingException {     MimeMessage message = mailSender.createMimeMessage();     MimeMessageHelper helper = new MimeMessageHelper(message, true);     helper.setFrom("your-email@gmail.com");     helper.setTo(to);     helper.setSubject(subject);     helper.setText(content, true);     mailSender.send(message); } 

八、总结

通过本文的介绍,我们了解了如何在Spring Boot项目中整合Java Mail,实现发送邮件的功能。无论是简单的文本邮件,还是复杂的HTML邮件,Java Mail都能轻松应对。希望本文对你有所帮助,如果你有任何问题或建议,欢迎在评论区留言。

百万大学生都在用的AI写论文工具,篇篇无重复👉:AI写论文

相关内容

热门资讯

透视脚本!aapoker辅助插... 透视脚本!aapoker辅助插件工具,aapoker怎么拿好牌,2025新版技巧(有挂揭秘)1、aa...
透视能赢!aapoker透视脚... 透视能赢!aapoker透视脚本下载,aapoker辅助软件合法吗,靠谱教程(有挂规律);aapok...
透视软件!aapoker插件下... 透视软件!aapoker插件下载,aapoker怎么提高中牌率,曝光教程(有挂解说);1、首先打开a...
透视辅助!aapoker插件下... 透视辅助!aapoker插件下载,aapoker能控制牌吗,辅助教程(有挂攻略)aapoker能控制...
透视安装!aapoker辅助工... 透视安装!aapoker辅助工具安全吗,aapoker透视方法,科技教程(有挂教程);一、aapok...
透视脚本!aapoker怎么提... 透视脚本!aapoker怎么提高中牌率,aapoker辅助软件合法吗,技巧教程(有挂黑科技);小薇(...
透视规律!aapoker透视插... 透视规律!aapoker透视插件,aapoker辅助软件合法吗,微扑克教程(有挂插件)1、玩家可以在...
透视ai代打!aapoker透... 透视ai代打!aapoker透视插件,aapoker真的假的,揭秘教程(有挂规律)小薇(透视辅助)致...
透视科技!aapoker俱乐部... 透视科技!aapoker俱乐部靠谱吗,aapoker发牌逻辑,科技教程(有挂教程)1、实时aapok...
透视最新!aapoker怎么开... 透视最新!aapoker怎么开辅助器,aapoker能控制牌吗,AA德州教程(有挂攻略)1、不需要A...