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

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


1. 邮件服务集成

1.1 邮件服务简介

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

1.2 配置与依赖

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

<dependency>  
    <groupId>org.springframework.bootgroupId>  
    <artifactId>spring-boot-starter-mailartifactId>  
dependency>  

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

spring:  
  mail:  
    host: smtp.qq.com  # SMTP 服务器地址(例如:QQ 邮箱)  
    port: 587          # SMTP 服务器端口  
    username: [email protected]  # 发送者邮箱地址  
    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.

你可能感兴趣的:(spring,boot,java,后端)