阅读量:0
引言
在现代的企业应用中,邮件发送是一个非常常见的功能。无论是用户注册后的验证邮件,还是系统通知邮件,邮件服务都扮演着重要的角色。本文将介绍如何在Spring Boot项目中整合Java Mail,实现发送邮件的功能。
一、准备工作
在开始之前,我们需要准备以下内容:
- 一个Spring Boot项目
- 一个可用的SMTP邮件服务器(如Gmail、QQ邮箱等)
二、添加依赖
首先,在pom.xml
中添加Spring Boot Starter Mail依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies>
三、配置邮件服务器
在application.properties
或application.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写论文