|
@@ -0,0 +1,124 @@
|
|
|
+package com.echepei.utils.mail;
|
|
|
+
|
|
|
+import com.echepei.dto.mail.MailDto;
|
|
|
+import jakarta.activation.DataHandler;
|
|
|
+import jakarta.activation.DataSource;
|
|
|
+import jakarta.activation.FileDataSource;
|
|
|
+import jakarta.mail.*;
|
|
|
+import jakarta.mail.internet.InternetAddress;
|
|
|
+import jakarta.mail.internet.MimeBodyPart;
|
|
|
+import jakarta.mail.internet.MimeMessage;
|
|
|
+import jakarta.mail.internet.MimeMultipart;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Rain
|
|
|
+ */
|
|
|
+public class SendMailUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送邮件(通用方法)
|
|
|
+ *
|
|
|
+ * @param mailDto 邮件信息
|
|
|
+ * @param contentType 邮件内容类型(如 "text/plain" 或 "text/html")
|
|
|
+ * @param hasAttachments 是否包含附件
|
|
|
+ */
|
|
|
+ private static void sendMail(MailDto mailDto, String contentType, boolean hasAttachments) {
|
|
|
+ try {
|
|
|
+ Session session = MailUtil.getSession(mailDto);
|
|
|
+ MimeMessage message = createMimeMessage(session, mailDto, contentType, hasAttachments);
|
|
|
+ Transport.send(message);
|
|
|
+ } catch (MessagingException e) {
|
|
|
+ handleMessagingException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建 MimeMessage
|
|
|
+ */
|
|
|
+ private static MimeMessage createMimeMessage(Session session, MailDto mailDto, String contentType, boolean hasAttachments) throws MessagingException {
|
|
|
+ MimeMessage message = new MimeMessage(session);
|
|
|
+ message.setSubject(mailDto.getMailTitle());
|
|
|
+ message.setFrom(new InternetAddress(mailDto.getMailFrom()));
|
|
|
+ message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(mailDto.getMailTo().trim()));
|
|
|
+ Multipart multipart = new MimeMultipart();
|
|
|
+ // 添加邮件正文
|
|
|
+ MimeBodyPart textPart = new MimeBodyPart();
|
|
|
+ textPart.setContent(StringUtils.hasText(mailDto.getMailContent()) ? mailDto.getMailContent() : "", contentType);
|
|
|
+ multipart.addBodyPart(textPart);
|
|
|
+ // 添加附件
|
|
|
+ if (hasAttachments && !CollectionUtils.isEmpty(mailDto.getMailFilePath())) {
|
|
|
+ for (String filePath : mailDto.getMailFilePath()) {
|
|
|
+ multipart.addBodyPart(createAttachmentBodyPart(filePath));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ message.setContent(multipart);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建附件 BodyPart
|
|
|
+ */
|
|
|
+ private static MimeBodyPart createAttachmentBodyPart(String filePath) throws MessagingException {
|
|
|
+ MimeBodyPart filePart = new MimeBodyPart();
|
|
|
+ DataSource source = new FileDataSource(filePath);
|
|
|
+ filePart.setFileName(new File(filePath).getName());
|
|
|
+ filePart.setDataHandler(new DataHandler(source));
|
|
|
+ return filePart;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理 MessagingException
|
|
|
+ */
|
|
|
+ private static void handleMessagingException(MessagingException e) {
|
|
|
+ if (e.getMessage().contains("554 Reject by")) {
|
|
|
+ throw new RuntimeException("发信行为被反垃圾拦截,投递失败。");
|
|
|
+ }
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送纯文本邮件
|
|
|
+ */
|
|
|
+ public static void sendTextMail(MailDto mailDto) {
|
|
|
+ sendMail(mailDto, "text/plain", false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送 HTML 邮件
|
|
|
+ */
|
|
|
+ public static void sendHtmlMail(MailDto mailDto) {
|
|
|
+ sendMail(mailDto, "text/html;charset=utf-8", false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送带附件的邮件
|
|
|
+ */
|
|
|
+ public static void sendFileMail(MailDto mailDto) {
|
|
|
+ if (CollectionUtils.isEmpty(mailDto.getMailFilePath())) {
|
|
|
+ throw new RuntimeException("邮件附件为空");
|
|
|
+ }
|
|
|
+ sendMail(mailDto, "text/plain", true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送带附件的纯文本邮件
|
|
|
+ */
|
|
|
+ public static void sendTextFileMail(MailDto mailDto) {
|
|
|
+ sendFileMail(mailDto);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送带附件的 HTML 邮件
|
|
|
+ */
|
|
|
+ public static void sendHtmlFileMail(MailDto mailDto) {
|
|
|
+ if (CollectionUtils.isEmpty(mailDto.getMailFilePath())) {
|
|
|
+ throw new RuntimeException("邮件附件为空");
|
|
|
+ }
|
|
|
+ sendMail(mailDto, "text/html;charset=utf-8", true);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|