mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 17:08:40 +08:00
编写Html模板邮件发送方法
This commit is contained in:
parent
a45df3ea28
commit
1b8eed8f5d
@ -48,7 +48,17 @@ public interface MailClient {
|
|||||||
*/
|
*/
|
||||||
void sendEmail(List<String> mailAddress, String title, String body, String... files);
|
void sendEmail(List<String> mailAddress, String title, String body, String... files);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sendHtml
|
||||||
|
* <p> 读取模板发送html邮件,无正文
|
||||||
|
* <p> 将默认读取resources/template下的html文件,第三个参数为html的名称,需携带尾缀
|
||||||
|
* @param mailAddress 收件人地址
|
||||||
|
* @param title 邮件标题
|
||||||
|
* @param htmlName 邮件正文
|
||||||
|
* @param parameter key为模板的变量名称 无需携带大括号 value为模板变量所对应的值
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void sendHtml(String mailAddress, String title , String htmlName, Map<String,String> parameter);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sendHtml
|
* sendHtml
|
||||||
@ -59,8 +69,21 @@ public interface MailClient {
|
|||||||
* @param htmlName 邮件正文
|
* @param htmlName 邮件正文
|
||||||
* @param parameter key为模板的变量名称 无需携带大括号 value为模板变量所对应的值
|
* @param parameter key为模板的变量名称 无需携带大括号 value为模板变量所对应的值
|
||||||
* @author :Wind
|
* @author :Wind
|
||||||
*/
|
*/
|
||||||
void sendHtml(String mailAddress, String title , String htmlName, Map<String,Object> parameter);
|
void sendHtml(List<String> mailAddress, String title , String htmlName, Map<String,String> parameter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sendHtml
|
||||||
|
* <p> 读取模板发送html邮件,无正文
|
||||||
|
* <p> 将默认读取resources/template下的html文件,第三个参数为html的名称,需携带尾缀
|
||||||
|
* <p> 用户可以自己编写一个实体类,并实现Parameter接口,编写get和set方法,这样一来字段的名称则为模板变量名称,对象的值则为模板变量的值
|
||||||
|
* @param mailAddress 收件人地址
|
||||||
|
* @param title 邮件标题
|
||||||
|
* @param htmlName 邮件模板名称
|
||||||
|
* @param parameter 实体
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void sendHtml(String mailAddress, String title , String htmlName, Parameter parameter);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sendHtml
|
* sendHtml
|
||||||
@ -73,7 +96,20 @@ public interface MailClient {
|
|||||||
* @param parameter 实体
|
* @param parameter 实体
|
||||||
* @author :Wind
|
* @author :Wind
|
||||||
*/
|
*/
|
||||||
void sendHtml(String mailAddress, String title , String htmlName, Parameter parameter);
|
void sendHtml(List<String> mailAddress, String title , String htmlName, Parameter parameter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sendHtml
|
||||||
|
* <p> 读取模板发送html邮件,无正文,带附件
|
||||||
|
* <p> 将默认读取resources/template下的html文件,第三个参数为html的名称,需携带尾缀
|
||||||
|
* @param mailAddress 收件人地址
|
||||||
|
* @param title 邮件标题
|
||||||
|
* @param htmlName 邮件模板名称
|
||||||
|
* @param parameter 实体
|
||||||
|
* @param files 附件,可添加多个
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void sendHtml(String mailAddress, String title , String htmlName,Map<String,String> parameter,String...files);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sendHtml
|
* sendHtml
|
||||||
@ -86,14 +122,14 @@ public interface MailClient {
|
|||||||
* @param files 附件,可添加多个
|
* @param files 附件,可添加多个
|
||||||
* @author :Wind
|
* @author :Wind
|
||||||
*/
|
*/
|
||||||
void sendHtml(String mailAddress, String title , String htmlName,Map<String,Object> parameter,String...files);
|
void sendHtml(List<String> mailAddress, String title , String htmlName,Map<String,String> parameter,String...files);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sendHtml
|
* sendHtml
|
||||||
* <p> 读取模板发送html邮件,无正文,带附件
|
* <p> 读取模板发送html邮件,无正文,带附件
|
||||||
* <p> 将默认读取resources/template下的html文件,第三个参数为html的名称,需携带尾缀
|
* <p> 将默认读取resources/template下的html文件,第三个参数为html的名称,需携带尾缀
|
||||||
* <p> 用户可以自己编写一个实体类,并实现Parameter接口,编写get和set方法,这样一来字段的名称则为模板变量名称,对象的值则为模板变量的值
|
* <p> 用户可以自己编写一个实体类,并实现Parameter接口,编写get和set方法,这样一来字段的名称则为模板变量名称,对象的值则为模板变量的值
|
||||||
* @param mailAddress 收件人地址,添加多个
|
* @param mailAddress 收件人地址
|
||||||
* @param title 邮件标题
|
* @param title 邮件标题
|
||||||
* @param htmlName 邮件模板名称
|
* @param htmlName 邮件模板名称
|
||||||
* @param parameter 实体
|
* @param parameter 实体
|
||||||
@ -102,6 +138,33 @@ public interface MailClient {
|
|||||||
*/
|
*/
|
||||||
void sendHtml(String mailAddress, String title , String htmlName,Parameter parameter,String...files);
|
void sendHtml(String mailAddress, String title , String htmlName,Parameter parameter,String...files);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sendHtml
|
||||||
|
* <p> 读取模板发送html邮件,无正文,带附件
|
||||||
|
* <p> 将默认读取resources/template下的html文件,第三个参数为html的名称,需携带尾缀
|
||||||
|
* <p> 用户可以自己编写一个实体类,并实现Parameter接口,编写get和set方法,这样一来字段的名称则为模板变量名称,对象的值则为模板变量的值
|
||||||
|
* @param mailAddress 收件人地址,添加多个
|
||||||
|
* @param title 邮件标题
|
||||||
|
* @param htmlName 邮件模板名称
|
||||||
|
* @param parameter 实体
|
||||||
|
* @param files 附件,可添加多个
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void sendHtml(List<String> mailAddress, String title , String htmlName,Parameter parameter,String...files);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sendHtml
|
||||||
|
* <p> 读取模板发送html邮件,并携带正文
|
||||||
|
* <p> 将默认读取resources/template下的html文件,第四个参数为html的名称,需携带尾缀
|
||||||
|
* @param mailAddress 收件人地址
|
||||||
|
* @param title 邮件标题
|
||||||
|
* @param body 邮件文本正文
|
||||||
|
* @param htmlName 邮件正文
|
||||||
|
* @param parameter key为模板的变量名称 无需携带大括号 value为模板变量所对应的值
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void sendHtml(String mailAddress, String title ,String body, String htmlName, Map<String,String> parameter);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sendHtml
|
* sendHtml
|
||||||
* <p> 读取模板发送html邮件,并携带正文
|
* <p> 读取模板发送html邮件,并携带正文
|
||||||
@ -113,7 +176,21 @@ public interface MailClient {
|
|||||||
* @param parameter key为模板的变量名称 无需携带大括号 value为模板变量所对应的值
|
* @param parameter key为模板的变量名称 无需携带大括号 value为模板变量所对应的值
|
||||||
* @author :Wind
|
* @author :Wind
|
||||||
*/
|
*/
|
||||||
void sendHtml(String mailAddress, String title ,String body, String htmlName, Map<String,Object> parameter);
|
void sendHtml(List<String> mailAddress, String title ,String body, String htmlName, Map<String,String> parameter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sendHtml
|
||||||
|
* <p> 读取模板发送html邮件,并携带正文
|
||||||
|
* <p> 将默认读取resources/template下的html文件,第四个参数为html的名称,需携带尾缀
|
||||||
|
* <p> 用户可以自己编写一个实体类,并实现Parameter接口,编写get和set方法,这样一来字段的名称则为模板变量名称,对象的值则为模板变量的值
|
||||||
|
* @param mailAddress 收件人地址
|
||||||
|
* @param title 邮件标题
|
||||||
|
* @param body 邮件文本正文
|
||||||
|
* @param htmlName 邮件正文
|
||||||
|
* @param parameter 实体
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void sendHtml(String mailAddress, String title ,String body, String htmlName, Parameter parameter);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sendHtml
|
* sendHtml
|
||||||
@ -127,7 +204,21 @@ public interface MailClient {
|
|||||||
* @param parameter 实体
|
* @param parameter 实体
|
||||||
* @author :Wind
|
* @author :Wind
|
||||||
*/
|
*/
|
||||||
void sendHtml(String mailAddress, String title ,String body, String htmlName, Parameter parameter);
|
void sendHtml(List<String> mailAddress, String title ,String body, String htmlName, Parameter parameter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sendHtml
|
||||||
|
* <p> 读取模板发送html邮件,并携带正文和附件
|
||||||
|
* <p> 将默认读取resources/template下的html文件,第四个参数为html的名称,需携带尾缀
|
||||||
|
* @param mailAddress 收件人地址
|
||||||
|
* @param title 邮件标题
|
||||||
|
* @param body 邮件文本正文
|
||||||
|
* @param htmlName 邮件正文
|
||||||
|
* @param parameter key为模板的变量名称 无需携带大括号 value为模板变量所对应的值
|
||||||
|
* @param files 附件,可添加多个
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void sendHtml(String mailAddress, String title ,String body, String htmlName, Map<String,String> parameter,String...files);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sendHtml
|
* sendHtml
|
||||||
@ -141,7 +232,23 @@ public interface MailClient {
|
|||||||
* @param files 附件,可添加多个
|
* @param files 附件,可添加多个
|
||||||
* @author :Wind
|
* @author :Wind
|
||||||
*/
|
*/
|
||||||
void sendHtml(String mailAddress, String title ,String body, String htmlName, Map<String,Object> parameter,String...files);
|
void sendHtml(List<String> mailAddress, String title ,String body, String htmlName, Map<String,String> parameter,String...files);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sendHtml
|
||||||
|
* <p> 读取模板发送html邮件,并携带正文和附件
|
||||||
|
* <p> 将默认读取resources/template下的html文件,第四个参数为html的名称,需携带尾缀
|
||||||
|
* <p> 用户可以自己编写一个实体类,并实现Parameter接口,编写get和set方法,这样一来字段的名称则为模板变量名称,对象的值则为模板变量的值
|
||||||
|
* @param mailAddress 收件人地址
|
||||||
|
* @param title 邮件标题
|
||||||
|
* @param body 邮件文本正文
|
||||||
|
* @param htmlName 邮件正文
|
||||||
|
* @param parameter key为模板的变量名称 无需携带大括号 value为模板变量所对应的值
|
||||||
|
* @param files 附件,可添加多个
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void sendHtml(String mailAddress, String title ,String body, String htmlName, Parameter parameter,String...files);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sendHtml
|
* sendHtml
|
||||||
@ -156,7 +263,7 @@ public interface MailClient {
|
|||||||
* @param files 附件,可添加多个
|
* @param files 附件,可添加多个
|
||||||
* @author :Wind
|
* @author :Wind
|
||||||
*/
|
*/
|
||||||
void sendHtml(String mailAddress, String title ,String body, String htmlName, Parameter parameter,String...files);
|
void sendHtml(List<String> mailAddress, String title ,String body, String htmlName, Parameter parameter,String...files);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
package org.dromara.email.comm.utils;
|
package org.dromara.email.core;
|
||||||
|
|
||||||
|
import org.dromara.email.api.Parameter;
|
||||||
import org.dromara.email.comm.errors.MailException;
|
import org.dromara.email.comm.errors.MailException;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Parameter;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -19,14 +19,14 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 将对象的属性和属性值变为map
|
* 将对象的属性和属性值变为map
|
||||||
* */
|
* */
|
||||||
public static Map<String, Object> getValues(Parameter parameter) {
|
public static Map<String, String> getValues(Parameter parameter) {
|
||||||
try {
|
try {
|
||||||
Map<String ,Object> map = new HashMap<>();
|
Map<String ,String> map = new HashMap<>();
|
||||||
Class<?> clazz = Class.forName(getObjectName(parameter));
|
Class<?> clazz = Class.forName(getObjectName(parameter));
|
||||||
Field[] declaredFields = clazz.getDeclaredFields();
|
Field[] declaredFields = clazz.getDeclaredFields();
|
||||||
for (Field declaredField : declaredFields) {
|
for (Field declaredField : declaredFields) {
|
||||||
declaredField.setAccessible(true);
|
declaredField.setAccessible(true);
|
||||||
map.put(declaredField.getName(),declaredField.get(parameter));
|
map.put(declaredField.getName(), (String) declaredField.get(parameter));
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -1,11 +1,12 @@
|
|||||||
package org.dromara.email.core.service;
|
package org.dromara.email.core.service;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.dromara.email.api.Blacklist;
|
|
||||||
import org.dromara.email.api.MailClient;
|
import org.dromara.email.api.MailClient;
|
||||||
import org.dromara.email.api.Parameter;
|
import org.dromara.email.api.Parameter;
|
||||||
import org.dromara.email.comm.errors.MailException;
|
import org.dromara.email.comm.errors.MailException;
|
||||||
import org.dromara.email.comm.utils.BaseUtil;
|
import org.dromara.email.comm.utils.BaseUtil;
|
||||||
|
import org.dromara.email.comm.utils.HtmlUtil;
|
||||||
|
import org.dromara.email.core.ReflectUtil;
|
||||||
|
|
||||||
import javax.activation.DataHandler;
|
import javax.activation.DataHandler;
|
||||||
import javax.activation.DataSource;
|
import javax.activation.DataSource;
|
||||||
@ -32,17 +33,17 @@ public class MailService implements MailClient {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMail(String mailAddress, String title, String body) {
|
public void sendMail(String mailAddress, String title, String body) {
|
||||||
sendEmail(mailAddress,title,body);
|
sendEmail(mailAddress, title, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMail(List<String> mailAddress, String title, String body) {
|
public void sendMail(List<String> mailAddress, String title, String body) {
|
||||||
sendEmail(mailAddress,title,body);
|
sendEmail(mailAddress, title, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendEmail(String mailAddress, String title, String body, String... files) {
|
public void sendEmail(String mailAddress, String title, String body, String... files) {
|
||||||
sendEmail(Collections.singletonList(mailAddress),title,body,files);
|
sendEmail(Collections.singletonList(mailAddress), title, body, files);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -53,14 +54,7 @@ public class MailService implements MailClient {
|
|||||||
message.setSubject(title);
|
message.setSubject(title);
|
||||||
message.setText(body);
|
message.setText(body);
|
||||||
Multipart multipart = new MimeMultipart();
|
Multipart multipart = new MimeMultipart();
|
||||||
for (String file : files) {
|
forFiles(multipart, files);
|
||||||
// 设置附件消息部分
|
|
||||||
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
|
||||||
DataSource source = new FileDataSource(file);
|
|
||||||
messageBodyPart.setDataHandler(new DataHandler(source));
|
|
||||||
messageBodyPart.setFileName(BaseUtil.getPathName(file));
|
|
||||||
multipart.addBodyPart(messageBodyPart);
|
|
||||||
}
|
|
||||||
message.setContent(multipart);
|
message.setContent(multipart);
|
||||||
|
|
||||||
Transport.send(message);
|
Transport.send(message);
|
||||||
@ -70,92 +64,112 @@ public class MailService implements MailClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHtml(String mailAddress, String title, String htmlName, Map<String, Object> parameter) {
|
public void sendHtml(String mailAddress, String title, String htmlName, Map<String, String> parameter) {
|
||||||
|
sendHtml(Collections.singletonList(mailAddress), title, htmlName, parameter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendHtml(List<String> mailAddress, String title, String htmlName, Map<String, String> parameter) {
|
||||||
|
sendHtml(mailAddress, title, htmlName, parameter, new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHtml(String mailAddress, String title, String htmlName, Parameter parameter) {
|
public void sendHtml(String mailAddress, String title, String htmlName, Parameter parameter) {
|
||||||
|
sendHtml(Collections.singletonList(mailAddress), title, htmlName, parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHtml(String mailAddress, String title, String htmlName, Map<String, Object> parameter, String... files) {
|
public void sendHtml(List<String> mailAddress, String title, String htmlName, Parameter parameter) {
|
||||||
|
sendHtml(mailAddress, title, htmlName, ReflectUtil.getValues(parameter));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendHtml(String mailAddress, String title, String htmlName, Map<String, String> parameter, String... files) {
|
||||||
|
sendHtml(mailAddress, title, "", htmlName, parameter, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendHtml(List<String> mailAddress, String title, String htmlName, Map<String, String> parameter, String... files) {
|
||||||
|
sendHtml(mailAddress, title, "", htmlName, parameter, files);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHtml(String mailAddress, String title, String htmlName, Parameter parameter, String... files) {
|
public void sendHtml(String mailAddress, String title, String htmlName, Parameter parameter, String... files) {
|
||||||
|
sendHtml(mailAddress, title, htmlName, ReflectUtil.getValues(parameter), files);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHtml(String mailAddress, String title, String body, String htmlName, Map<String, Object> parameter) {
|
public void sendHtml(List<String> mailAddress, String title, String htmlName, Parameter parameter, String... files) {
|
||||||
|
sendHtml(mailAddress, title, htmlName, ReflectUtil.getValues(parameter), files);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendHtml(String mailAddress, String title, String body, String htmlName, Map<String, String> parameter) {
|
||||||
|
sendHtml(mailAddress, title, body, htmlName, parameter, new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendHtml(List<String> mailAddress, String title, String body, String htmlName, Map<String, String> parameter) {
|
||||||
|
sendHtml(mailAddress, title, body, htmlName, parameter, new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHtml(String mailAddress, String title, String body, String htmlName, Parameter parameter) {
|
public void sendHtml(String mailAddress, String title, String body, String htmlName, Parameter parameter) {
|
||||||
|
sendHtml(Collections.singletonList(mailAddress), title, body, htmlName, parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHtml(String mailAddress, String title, String body, String htmlName, Map<String, Object> parameter, String... files) {
|
public void sendHtml(List<String> mailAddress, String title, String body, String htmlName, Parameter parameter) {
|
||||||
|
sendHtml(mailAddress, title, body, htmlName, parameter, new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHtml(String mailAddress, String title, String body, String htmlName, Parameter parameter, String... files) {
|
public void sendHtml(String mailAddress, String title, String body, String htmlName, Map<String, String> parameter, String... files) {
|
||||||
|
sendHtml(Collections.singletonList(mailAddress), title, body, htmlName, parameter, files);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHtml(String mailAddress, String Title, String body) {
|
public void sendHtml(List<String> mailAddress, String title, String body, String htmlName, Map<String, String> parameter, String... files) {
|
||||||
String smtpServer = "smtp.qq.com";
|
|
||||||
String username = "wzsf1810@qq.com";
|
|
||||||
String password = "xophzbzswkzkiacb";
|
|
||||||
String fromAddress = "wzsf1810@qq.com";
|
|
||||||
String toAddress = "291203727@qq.com";
|
|
||||||
int port = 465; // SMTP服务器的端口号
|
|
||||||
|
|
||||||
Properties props = new Properties();
|
|
||||||
props.put("mail.smtp.host", smtpServer);
|
|
||||||
props.put("mail.smtp.auth", "true");
|
|
||||||
props.put("mail.smtp.port", port);
|
|
||||||
props.put("mail.smtp.ssl.enable", "true");
|
|
||||||
// props.put("mail.smtp.ssl.socketFactory", new MailSSLSocketFactory());
|
|
||||||
|
|
||||||
Session session = Session.getInstance(props, new Authenticator() {
|
|
||||||
protected PasswordAuthentication getPasswordAuthentication() {
|
|
||||||
return new PasswordAuthentication(username, password);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Message message = new MimeMessage(session);
|
Message message = mailBuild.getMessage();
|
||||||
message.setFrom(new InternetAddress(fromAddress));
|
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(mailAddress));
|
||||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
|
message.setSubject(title);
|
||||||
message.setSubject(Title);
|
message.setText(body);
|
||||||
// 创建 MimeMultipart 对象
|
Multipart multipart = new MimeMultipart();
|
||||||
MimeMultipart multipart = new MimeMultipart("related");
|
//读取模板并进行变量替换
|
||||||
// 创建 MimeBodyPart 对象,并将 HTML 内容添加到 MimeMultipart 中
|
List<String> strings = HtmlUtil.replacePlaceholder(HtmlUtil.readHtml(htmlName), parameter);
|
||||||
|
//拼合HTML数据
|
||||||
|
String htmlData = HtmlUtil.pieceHtml(strings);
|
||||||
|
//添加附件
|
||||||
|
forFiles(multipart, files);
|
||||||
MimeBodyPart htmlPart = new MimeBodyPart();
|
MimeBodyPart htmlPart = new MimeBodyPart();
|
||||||
htmlPart.setContent(body, "text/html;charset=UTF-8");
|
htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
|
||||||
message.setContent(multipart);
|
|
||||||
multipart.addBodyPart(htmlPart);
|
multipart.addBodyPart(htmlPart);
|
||||||
|
message.setContent(multipart);
|
||||||
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
|
||||||
String filename = "file.txt";
|
|
||||||
DataSource source = new FileDataSource(filename);
|
|
||||||
messageBodyPart.setDataHandler(new DataHandler(source));
|
|
||||||
messageBodyPart.setFileName(filename);
|
|
||||||
multipart.addBodyPart(messageBodyPart);
|
|
||||||
|
|
||||||
|
|
||||||
Transport.send(message);
|
Transport.send(message);
|
||||||
System.out.println("Email sent successfully.");
|
|
||||||
} catch (MessagingException e) {
|
} catch (MessagingException e) {
|
||||||
throw new MailException(e);
|
throw new MailException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendHtml(String mailAddress, String title, String body, String htmlName, Parameter parameter, String... files) {
|
||||||
|
sendHtml(Collections.singletonList(mailAddress), title, body, htmlName, parameter, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendHtml(List<String> mailAddress, String title, String body, String htmlName, Parameter parameter, String... files) {
|
||||||
|
sendHtml(mailAddress, title, body, htmlName, ReflectUtil.getValues(parameter), files);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void forFiles(Multipart multipart, String[] files) throws MessagingException {
|
||||||
|
for (String file : files) {
|
||||||
|
// 设置附件消息部分
|
||||||
|
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
||||||
|
DataSource source = new FileDataSource(file);
|
||||||
|
messageBodyPart.setDataHandler(new DataHandler(source));
|
||||||
|
messageBodyPart.setFileName(BaseUtil.getPathName(file));
|
||||||
|
multipart.addBodyPart(messageBodyPart);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user