mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 08:58:38 +08:00
修改 读取html模板时存在多个变量会产生替换异常的问题
修改 body和附件添加空判断,防止出现邮件内容为空的问题
This commit is contained in:
parent
568a93c415
commit
058f640361
@ -1,11 +1,15 @@
|
||||
package org.dromara.email.comm.utils;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class BaseUtil {
|
||||
|
||||
public static String getPathName(String path) {
|
||||
String[] split = path.split(File.separator);
|
||||
return split[split.length-1];
|
||||
}
|
||||
/**
|
||||
* getPathName
|
||||
* <p>分隔文件路径,并获取文件名
|
||||
* @param path 文件路径
|
||||
* @author :Wind
|
||||
*/
|
||||
public static String getPathName(String path) {
|
||||
String[] split = path.split("/");
|
||||
return split[split.length - 1];
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ import java.util.Objects;
|
||||
/**
|
||||
* HtmlUtil
|
||||
* <p> Html相关工具
|
||||
*
|
||||
* @author :Wind
|
||||
* 2023/6/7 20:15
|
||||
**/
|
||||
@ -30,7 +31,7 @@ public final class HtmlUtil {
|
||||
* @author :Wind
|
||||
*/
|
||||
public static List<String> readHtml(String name) throws MailException {
|
||||
try (InputStream is = htmlUtil.getClass().getResourceAsStream("/template/" + name);) {
|
||||
try (InputStream is = HtmlUtil.class.getResourceAsStream("/template/" + name);) {
|
||||
return readHtml(is);
|
||||
} catch (IOException e) {
|
||||
throw new MailException(e);
|
||||
@ -77,49 +78,48 @@ public final class HtmlUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* replacePlaceholder
|
||||
* replacePlaceholder
|
||||
* <p>将所包含占位符的字符串替换为固定值
|
||||
* @param data 源数据
|
||||
*
|
||||
* @param data 源数据
|
||||
* @param parameter key为占位符名称 value为占位符应替换的值
|
||||
* @author :Wind
|
||||
*/
|
||||
*/
|
||||
public static List<String> replacePlaceholder(List<String> data, Map<String, String> parameter) {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (String datum : data) {
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
for (Map.Entry<String, String> s : parameter.entrySet()) {
|
||||
String piece = piece(s.getKey());
|
||||
if (datum.contains(piece)){
|
||||
list.add(datum.replace(piece, s.getValue()));
|
||||
}else {
|
||||
list.add(datum);
|
||||
if (data.get(i).contains(piece)){
|
||||
data.set(i,s.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return list;
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* pieceHtml
|
||||
* pieceHtml
|
||||
* <p>将数据拼合为html
|
||||
*
|
||||
* @param data 需要拼合的数据
|
||||
* @author :Wind
|
||||
*/
|
||||
public static String pieceHtml(List<String> data){
|
||||
*/
|
||||
public static String pieceHtml(List<String> data) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String datum : data) {
|
||||
sb.append(datum);
|
||||
sb.append("\r\n");
|
||||
sb.append(datum);
|
||||
sb.append("\r\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* piece
|
||||
* piece
|
||||
* <p>将参数拼合为完整占位符
|
||||
*
|
||||
* @author :Wind
|
||||
*/
|
||||
public static String piece(String parameter){
|
||||
return "#{"+parameter+"}";
|
||||
*/
|
||||
public static String piece(String parameter) {
|
||||
return "#{" + parameter + "}";
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,6 +19,8 @@ import javax.mail.internet.MimeMultipart;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class MailService implements MailClient {
|
||||
|
||||
private MailBuild mailBuild;
|
||||
@ -52,11 +54,14 @@ public class MailService implements MailClient {
|
||||
Message message = mailBuild.getMessage();
|
||||
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(mailAddress));
|
||||
message.setSubject(title);
|
||||
message.setText(body);
|
||||
Multipart multipart = new MimeMultipart();
|
||||
forFiles(multipart, files);
|
||||
message.setContent(multipart);
|
||||
|
||||
if (Objects.isNull(body) ||body.isEmpty()){
|
||||
message.setText(body);
|
||||
}
|
||||
if (files.length != 0){
|
||||
Multipart multipart = new MimeMultipart();
|
||||
forFiles(multipart, files);
|
||||
message.setContent(multipart);
|
||||
}
|
||||
Transport.send(message);
|
||||
} catch (MessagingException e) {
|
||||
throw new MailException(e);
|
||||
@ -134,14 +139,22 @@ public class MailService implements MailClient {
|
||||
Message message = mailBuild.getMessage();
|
||||
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(mailAddress));
|
||||
message.setSubject(title);
|
||||
message.setText(body);
|
||||
Multipart multipart = new MimeMultipart();
|
||||
|
||||
Multipart multipart = new MimeMultipart("alternative");
|
||||
//读取模板并进行变量替换
|
||||
List<String> strings = HtmlUtil.replacePlaceholder(HtmlUtil.readHtml(htmlName), parameter);
|
||||
//拼合HTML数据
|
||||
String htmlData = HtmlUtil.pieceHtml(strings);
|
||||
if (!body.isEmpty()){
|
||||
// 创建文本正文部分
|
||||
MimeBodyPart textPart = new MimeBodyPart();
|
||||
textPart.setText(body);
|
||||
multipart.addBodyPart(textPart);
|
||||
}
|
||||
//添加附件
|
||||
forFiles(multipart, files);
|
||||
if (files.length != 0){
|
||||
forFiles(multipart, files);
|
||||
}
|
||||
MimeBodyPart htmlPart = new MimeBodyPart();
|
||||
htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
|
||||
multipart.addBodyPart(htmlPart);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user