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

@ -24,7 +24,7 @@ public class MailFactory{
* <p>从工厂获取一个邮件发送实例
* @param key 配置的标识key
* @author :Wind
*/
*/
public static MailClient createMailClient(Object key){
try {
return MailBuild.build(CONFIGS.get(key));
@ -40,7 +40,7 @@ public class MailFactory{
* @param key 配置的标识key
* @param blacklist 黑名单接口实例将从这里获取黑名单数据
* @author :Wind
*/
*/
public static MailClient createMailClient(Object key, Blacklist blacklist){
try {
return MailBuild.build(CONFIGS.get(key),blacklist);
@ -55,7 +55,7 @@ public class MailFactory{
* @param key 标识
* @param config 配置对象
* @author :Wind
*/
*/
public static void put(Object key, MailSmtpConfig config){
CONFIGS.put(key,config);
}

View File

@ -28,7 +28,7 @@ public class MonitorFactory {
* @param config 监听配置
* @param monitor 回调对象
* @author :Wind
*/
*/
public static void put(String key, MailImapConfig config, Monitor monitor){
SERVICES.put(key,new MonitorService(config,monitor));
}
@ -38,9 +38,9 @@ public class MonitorFactory {
* <p> 开始监听指定标识的邮箱
* @param key 标识
* @author :Wind
*/
*/
public static void start(String key){
SERVICES.get(key).start();
SERVICES.get(key).start();
}
/**
@ -48,7 +48,7 @@ public class MonitorFactory {
* <p> 停止监听指定标识的邮箱
* @param key 标识
* @author :Wind
*/
*/
public static void stop(String key){
SERVICES.get(key).stop();
}
@ -58,7 +58,7 @@ public class MonitorFactory {
* <p> 获取指定标识的配置信息
* @param key 标识
* @author :Wind
*/
*/
public static MailImapConfig getConfig(String key) {
return SERVICES.get(key).getMailImapConfig();
}

View File

@ -99,7 +99,7 @@ public class MailBuild {
return MailService.instance(new MailBuild(config));
}
public static MailClient build(MailSmtpConfig config,Blacklist blacklist)throws MessagingException {
return MailService.instance(new MailBuild(config,blacklist));
return MailService.instance(new MailBuild(config,blacklist));
}
/**
@ -120,7 +120,7 @@ public class MailBuild {
list.add(s);
}
}
return InternetAddress.parse(CollUtil.join(list, ","));
return InternetAddress.parse(CollUtil.join(list, ","));
} catch (AddressException e) {
throw new MailException(e);
}

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;
@ -50,8 +51,14 @@ public class MailService implements MailClient {
if (mailMessage.getHtmlInputStream() != null) {
html = HtmlUtil.readHtml(mailMessage.getHtmlInputStream());
}
if (StrUtil.isNotBlank(mailMessage.getHtmlPath())){
html = HtmlUtil.readHtml(mailMessage.getHtmlPath());
if (StrUtil.isNotBlank(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)) {
//读取模板并进行变量替换
List<String> strings = HtmlUtil.replacePlaceholder(html, parameter);
//拼合HTML数据
String htmlData = HtmlUtil.pieceHtml(strings);
if (CollUtil.isNotEmpty(html)) {
String htmlData;
List<String> strings;
if (MapUtil.isNotEmpty(parameter)) {
//读取模板并进行变量替换
strings = HtmlUtil.replacePlaceholder(html, parameter);
//拼合HTML数据
htmlData = HtmlUtil.pieceHtml(strings);
}else {
htmlData = HtmlUtil.pieceHtml(html);
}
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
multipart.addBodyPart(htmlPart);