feat(email): 添加HTML格式邮件发送功能
- 在EmailService接口中新增sendHtmlMail方法定义 - 实现SMTP邮件服务中的HTML邮件发送逻辑 - 添加HTML邮件内容的MIME消息构建处理 - 集成UTF-8字符编码支持HTML邮件内容 - 添加HTML邮件发送异常日志记录机制
This commit is contained in:
@@ -19,6 +19,15 @@ public interface EmailService {
|
|||||||
*/
|
*/
|
||||||
void send(String to, String subject, String text);
|
void send(String to, String subject, String text);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送HTML格式的邮件。
|
||||||
|
*
|
||||||
|
* @param to 收件人邮箱
|
||||||
|
* @param subject 邮件主题
|
||||||
|
* @param html 邮件正文(HTML 格式)
|
||||||
|
*/
|
||||||
|
void sendHtmlMail(String to, String subject, String html);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送带附件的邮件。
|
* 发送带附件的邮件。
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package com.youlai.boot.support.mail.service.impl;
|
|||||||
import com.youlai.boot.support.mail.MailProperties;
|
import com.youlai.boot.support.mail.MailProperties;
|
||||||
import com.youlai.boot.support.mail.service.EmailService;
|
import com.youlai.boot.support.mail.service.EmailService;
|
||||||
import jakarta.mail.MessagingException;
|
import jakarta.mail.MessagingException;
|
||||||
|
import jakarta.mail.internet.MimeBodyPart;
|
||||||
import jakarta.mail.internet.MimeMessage;
|
import jakarta.mail.internet.MimeMessage;
|
||||||
|
import jakarta.mail.internet.MimeMultipart;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
@@ -14,6 +16,7 @@ import org.springframework.mail.javamail.MimeMessageHelper;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SMTP 邮件服务。
|
* SMTP 邮件服务。
|
||||||
@@ -50,6 +53,36 @@ public class SmtpEmailServiceImpl implements EmailService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送HTML格式的邮件
|
||||||
|
*
|
||||||
|
* @param to 收件人地址
|
||||||
|
* @param subject 邮件主题
|
||||||
|
* @param html html格式的内容
|
||||||
|
* @return 是否发送成功
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void sendHtmlMail(String to, String subject, String html) {
|
||||||
|
try {
|
||||||
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
|
// 设置发件人、收件人和主题
|
||||||
|
message.setFrom(mailProperties.getFrom());
|
||||||
|
message.setRecipients(jakarta.mail.Message.RecipientType.TO, to);
|
||||||
|
message.setSubject(subject, StandardCharsets.UTF_8.name());
|
||||||
|
// 创建 MimeMultipart 容器
|
||||||
|
MimeMultipart multipart = new MimeMultipart("related");
|
||||||
|
// 创建 HTML 内容部分
|
||||||
|
MimeBodyPart htmlPart = new MimeBodyPart();
|
||||||
|
htmlPart.setContent(html, "text/html; charset=UTF-8");
|
||||||
|
multipart.addBodyPart(htmlPart);
|
||||||
|
// 设置消息内容
|
||||||
|
message.setContent(multipart);
|
||||||
|
mailSender.send(message);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("发送Html邮件失败{}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送带附件的 HTML 邮件。
|
* 发送带附件的 HTML 邮件。
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user