mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-08 01:48:38 +08:00
fix 两个邮件插件核心代码不同步 #I9CWJI
This commit is contained in:
parent
30c4488cde
commit
109f50cfd1
@ -0,0 +1,6 @@
|
|||||||
|
/**
|
||||||
|
* <p> 邮件插件api模块
|
||||||
|
* @author :Wind
|
||||||
|
* 2024/10/23 10:58
|
||||||
|
**/
|
||||||
|
package org.dromara.email.jakarta.api;
|
||||||
@ -3,6 +3,7 @@ package org.dromara.email.jakarta.comm.entity;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.dromara.email.jakarta.comm.utils.ReflectUtil;
|
import org.dromara.email.jakarta.comm.utils.ReflectUtil;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -27,6 +28,12 @@ public class MailMessage {
|
|||||||
/** html模板文件的输入流,可来自任意可读取位置*/
|
/** html模板文件的输入流,可来自任意可读取位置*/
|
||||||
private InputStream htmlInputStream;
|
private InputStream htmlInputStream;
|
||||||
|
|
||||||
|
/** html内容,可以存在模板变量*/
|
||||||
|
private String htmlContent;
|
||||||
|
|
||||||
|
/** html 模板文件的File对象*/
|
||||||
|
private File htmlFile;
|
||||||
|
|
||||||
/** html 模板参数*/
|
/** html 模板参数*/
|
||||||
private Map<String,String> htmlValues;
|
private Map<String,String> htmlValues;
|
||||||
|
|
||||||
@ -141,6 +148,18 @@ public class MailMessage {
|
|||||||
return this;
|
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 模板参数*/
|
/** html 模板参数*/
|
||||||
public MailsBuilder htmlValues(String key, String value){
|
public MailsBuilder htmlValues(String key, String value){
|
||||||
if (mailMessage.htmlValues == null){
|
if (mailMessage.htmlValues == null){
|
||||||
|
|||||||
@ -0,0 +1,6 @@
|
|||||||
|
/**
|
||||||
|
* <p> 邮件插件通用模块
|
||||||
|
* @author :Wind
|
||||||
|
* 2024/10/23 10:58
|
||||||
|
**/
|
||||||
|
package org.dromara.email.jakarta.comm;
|
||||||
@ -2,11 +2,7 @@ package org.dromara.email.jakarta.comm.utils;
|
|||||||
|
|
||||||
import org.dromara.email.jakarta.comm.errors.MailException;
|
import org.dromara.email.jakarta.comm.errors.MailException;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -32,8 +28,8 @@ public final class HtmlUtil {
|
|||||||
* @param name 模板文件名
|
* @param name 模板文件名
|
||||||
* @author :Wind
|
* @author :Wind
|
||||||
*/
|
*/
|
||||||
public static List<String> readHtml(String name) throws MailException {
|
public static List<String> readHtml(String name,Class<?> clazz) throws MailException {
|
||||||
try (InputStream is = HtmlUtil.class.getResourceAsStream("/template/" + name)) {
|
try (InputStream is = clazz.getResourceAsStream("/template/" + name)) {
|
||||||
return readHtml(is);
|
return readHtml(is);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new MailException(e);
|
throw new MailException(e);
|
||||||
@ -75,6 +71,12 @@ public final class HtmlUtil {
|
|||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new MailException(e);
|
throw new MailException(e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
inputStream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,11 +11,7 @@ import java.io.FileInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.Channels;
|
import java.nio.channels.*;
|
||||||
import java.nio.channels.FileChannel;
|
|
||||||
import java.nio.channels.Pipe;
|
|
||||||
import java.nio.channels.ReadableByteChannel;
|
|
||||||
import java.nio.channels.WritableByteChannel;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
|
|||||||
@ -26,6 +26,7 @@ import org.dromara.email.jakarta.comm.utils.ZipUtils;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@ -51,7 +52,13 @@ public class MailService implements MailClient {
|
|||||||
html = HtmlUtil.readHtml(mailMessage.getHtmlInputStream());
|
html = HtmlUtil.readHtml(mailMessage.getHtmlInputStream());
|
||||||
}
|
}
|
||||||
if (StrUtil.isNotBlank(mailMessage.getHtmlPath())) {
|
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(),
|
send(mailMessage.getMailAddress(),
|
||||||
mailMessage.getTitle(),
|
mailMessage.getTitle(),
|
||||||
@ -186,11 +193,17 @@ public class MailService implements MailClient {
|
|||||||
message.setSubject(title);
|
message.setSubject(title);
|
||||||
|
|
||||||
Multipart multipart = new MimeMultipart("alternative");
|
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数据
|
//拼合HTML数据
|
||||||
String htmlData = HtmlUtil.pieceHtml(strings);
|
htmlData = HtmlUtil.pieceHtml(strings);
|
||||||
|
}else {
|
||||||
|
htmlData = HtmlUtil.pieceHtml(html);
|
||||||
|
}
|
||||||
MimeBodyPart htmlPart = new MimeBodyPart();
|
MimeBodyPart htmlPart = new MimeBodyPart();
|
||||||
htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
|
htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
|
||||||
multipart.addBodyPart(htmlPart);
|
multipart.addBodyPart(htmlPart);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user