fix 两个邮件插件核心代码不同步 #I9CWJI

This commit is contained in:
bleachtred 2024-10-23 13:21:58 +08:00
parent 30c4488cde
commit 109f50cfd1
9 changed files with 71 additions and 29 deletions

View File

@ -0,0 +1,6 @@
/**
* <p> 邮件插件api模块
* @author :Wind
* 2024/10/23 10:58
**/
package org.dromara.email.jakarta.api;

View File

@ -3,6 +3,7 @@ package org.dromara.email.jakarta.comm.entity;
import lombok.Getter;
import org.dromara.email.jakarta.comm.utils.ReflectUtil;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
@ -27,6 +28,12 @@ public class MailMessage {
/** html模板文件的输入流可来自任意可读取位置*/
private InputStream htmlInputStream;
/** html内容可以存在模板变量*/
private String htmlContent;
/** html 模板文件的File对象*/
private File htmlFile;
/** html 模板参数*/
private Map<String,String> htmlValues;
@ -141,6 +148,18 @@ public class MailMessage {
return this;
}
/** html模板文件的File对象*/
public MailsBuilder html(File htmlFile){
mailMessage.htmlFile = htmlFile;
return this;
}
/** html内容直接输入*/
public MailsBuilder htmlContent(String htmlContent){
mailMessage.htmlContent = htmlContent;
return this;
}
/** html 模板参数*/
public MailsBuilder htmlValues(String key, String value){
if (mailMessage.htmlValues == null){

View File

@ -0,0 +1,6 @@
/**
* <p> 邮件插件通用模块
* @author :Wind
* 2024/10/23 10:58
**/
package org.dromara.email.jakarta.comm;

View File

@ -2,11 +2,7 @@ package org.dromara.email.jakarta.comm.utils;
import org.dromara.email.jakarta.comm.errors.MailException;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
@ -32,8 +28,8 @@ public final class HtmlUtil {
* @param name 模板文件名
* @author :Wind
*/
public static List<String> readHtml(String name) throws MailException {
try (InputStream is = HtmlUtil.class.getResourceAsStream("/template/" + name)) {
public static List<String> readHtml(String name,Class<?> clazz) throws MailException {
try (InputStream is = clazz.getResourceAsStream("/template/" + name)) {
return readHtml(is);
} catch (IOException e) {
throw new MailException(e);
@ -75,6 +71,12 @@ public final class HtmlUtil {
}
} catch (IOException e) {
throw new MailException(e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}

View File

@ -11,11 +11,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.Pipe;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.channels.*;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.zip.ZipEntry;

View File

@ -26,6 +26,7 @@ import org.dromara.email.jakarta.comm.utils.ZipUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -51,7 +52,13 @@ public class MailService implements MailClient {
html = HtmlUtil.readHtml(mailMessage.getHtmlInputStream());
}
if (StrUtil.isNotBlank(mailMessage.getHtmlPath())) {
html = HtmlUtil.readHtml(mailMessage.getHtmlPath());
html = HtmlUtil.readHtml(mailMessage.getHtmlPath(), MailService.class);
}
if (mailMessage.getHtmlFile() != null) {
html = HtmlUtil.readHtml(mailMessage.getHtmlFile());
}
if (StrUtil.isNotBlank(mailMessage.getHtmlContent())) {
html = Arrays.asList(mailMessage.getHtmlContent().split("\n"));
}
send(mailMessage.getMailAddress(),
mailMessage.getTitle(),
@ -186,11 +193,17 @@ public class MailService implements MailClient {
message.setSubject(title);
Multipart multipart = new MimeMultipart("alternative");
if (CollUtil.isNotEmpty(html) && MapUtil.isNotEmpty(parameter)) {
if (CollUtil.isNotEmpty(html)) {
String htmlData;
List<String> strings;
if (MapUtil.isNotEmpty(parameter)) {
//读取模板并进行变量替换
List<String> strings = HtmlUtil.replacePlaceholder(html, parameter);
strings = HtmlUtil.replacePlaceholder(html, parameter);
//拼合HTML数据
String htmlData = HtmlUtil.pieceHtml(strings);
htmlData = HtmlUtil.pieceHtml(strings);
}else {
htmlData = HtmlUtil.pieceHtml(html);
}
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
multipart.addBodyPart(htmlPart);