欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 52、Spring Boot 详细讲义(九) Spring Boot 与第三方服务

52、Spring Boot 详细讲义(九) Spring Boot 与第三方服务

2025/4/19 7:49:25 来源:https://blog.csdn.net/qq_22910257/article/details/147308568  浏览:    关键词:52、Spring Boot 详细讲义(九) Spring Boot 与第三方服务

Spring Boot 与第三方服务集成详细讲义


1. 邮件服务集成

1.1 邮件服务简介

邮件服务是现代应用程序中常见的功能模块,主要用于用户注册验证、密码重置、通知邮件等场景。Spring Boot 提供了简便的方式来集成邮件服务,主要通过 JavaMail 的 API 来发送邮件。

1.2 配置与依赖

在项目中集成邮件服务之前,需要添加以下依赖:

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-mail</artifactId>  
</dependency>  

application.properties 文件中配置邮件服务的相关参数:

spring:  mail:  host: smtp.qq.com  # SMTP 服务器地址(例如:QQ 邮箱)  port: 587          # SMTP 服务器端口  username: xxx@qq.com  # 发送者邮箱地址  password: your_password  # 邮件密码(或授权码)  properties:  mail:  smtp:  auth: true  starttls: true  required: true  
1.3 实现邮件发送

通过 JavaMailSender 来发送邮件,以下是几种常见的邮件发送方式:

  1. 发送简单文本邮件
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.mail.SimpleMailMessage;  
import org.springframework.mail.javamail.JavaMailSender;  @Service  
public class MailService {  @Autowired  private JavaMailSender javaMailSender;  public void sendSimpleMail(String to, String subject, String content) {  SimpleMailMessage message = new SimpleMailMessage();  message.setTo(to);  message.setSubject(subject);  message.setText(content);  javaMailSender.send(message);  }  
}  
  1. 发送 HTML 邮件
import org.springframework.mail.javamail.MimeMessage;  
import javax.mail.MessagingException;  public void sendHtmlMail(String to, String subject, String htmlContent) throws MessagingException {  MimeMessage message = javaMailSender.createMimeMessage();  message.setTo(to);  message.setSubject(subject);  message.setContent(htmlContent, "text/html;charset=UTF-8");  javaMailSender.send(message);  
}  
  1. 发送带附件的邮件
import org.springframework.core.io.FileSystemResource;  
import java.io.File;  
import java.io.IOException;  public void sendAttachmentMail(String to, String subject, String content, String filePath) throws MessagingException, IOException {  MimeMessage message = javaMailSender.createMimeMessage();  message.setTo(to);  message.setSubject(subject);  message.setText(content);  FileSystemResource file = new FileSystemResource(new File(filePath));  message.addAttachment(file.getFilename(), file);  javaMailSender.send(message);  
}  
1.4 集成模板引擎(可选)

如果需要发送带有模板的邮件,可以集成模板引擎(如 Thymeleaf)。在 application.properties 中启用 Thymeleaf:

spring:  thymeleaf:  mode: HTML  encoding: UTF-8  

然后在邮件内容中使用 Thymeleaf 模板。例如,创建一个 mail-template.html 文件:

<!DOCTYPE html>  
<html xmlns:th="http://www.thymeleaf.org">  
<head>  <title>邮件模板</title>  
</head>  
<body>  <h2 th:text="${title}">标题</h2>  <p th:text="${content}">内容</p>  
</body>  
</html>  

在发送邮件时,使用 Thymeleaf 渲染模板:

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.FileSystemResource;  
import org.springframework.core.io.InputStreamSource;  
import org.springframework.core.io.Resource;  
import org.springframework.mail.javamail.MimeMessage;  
import org.thymeleaf.

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词