This commit is contained in:
Looly 2022-05-05 13:47:40 +08:00
parent 78a5c53d98
commit fd0948ca3c
3 changed files with 49 additions and 37 deletions

View File

@ -4,9 +4,9 @@ import cn.hutool.core.builder.Builder;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.text.StrUtil;
import javax.activation.DataHandler; import javax.activation.DataHandler;
import javax.activation.DataSource; import javax.activation.DataSource;
@ -248,41 +248,6 @@ public class Mail implements Builder<MimeMessage> {
return setAttachments(attachments); return setAttachments(attachments);
} }
/**
* 增加附件或图片附件使用{@link DataSource} 形式表示可以使用{@link FileDataSource}包装文件表示文件附件
*
* @param attachments 附件列表
* @return this
* @since 4.0.9
*/
public Mail setAttachments(final DataSource... attachments) {
if (ArrayUtil.isNotEmpty(attachments)) {
final Charset charset = this.mailAccount.getCharset();
MimeBodyPart bodyPart;
String nameEncoded;
try {
for (final DataSource attachment : attachments) {
bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(attachment));
nameEncoded = attachment.getName();
if (this.mailAccount.isEncodefilename()) {
nameEncoded = InternalMailUtil.encodeText(nameEncoded, charset);
}
// 普通附件文件名
bodyPart.setFileName(nameEncoded);
if (StrUtil.startWith(attachment.getContentType(), "image/")) {
// 图片附件用于正文中引用图片
bodyPart.setContentID(nameEncoded);
}
this.multipart.addBodyPart(bodyPart);
}
} catch (final MessagingException e) {
throw new MailException(e);
}
}
return this;
}
/** /**
* 增加图片图片的键对应到邮件模板中的占位字符串图片类型默认为"image/jpeg" * 增加图片图片的键对应到邮件模板中的占位字符串图片类型默认为"image/jpeg"
* *
@ -333,6 +298,42 @@ public class Mail implements Builder<MimeMessage> {
} }
} }
/**
* 增加附件或图片附件使用{@link DataSource} 形式表示可以使用{@link FileDataSource}包装文件表示文件附件
*
* @param attachments 附件列表
* @return this
* @since 4.0.9
*/
public Mail setAttachments(final DataSource... attachments) {
if (ArrayUtil.isNotEmpty(attachments)) {
final Charset charset = this.mailAccount.getCharset();
MimeBodyPart bodyPart;
String nameEncoded;
try {
for (final DataSource attachment : attachments) {
bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(attachment));
nameEncoded = attachment.getName();
if (this.mailAccount.isEncodefilename()) {
nameEncoded = InternalMailUtil.encodeText(nameEncoded, charset);
}
// 普通附件文件名
bodyPart.setFileName(nameEncoded);
if (StrUtil.startWith(attachment.getContentType(), "image/")) {
// 图片附件用于正文中引用图片
bodyPart.setContentID(nameEncoded);
bodyPart.setDisposition(MimeBodyPart.INLINE);
}
this.multipart.addBodyPart(bodyPart);
}
} catch (final MessagingException e) {
throw new MailException(e);
}
}
return this;
}
/** /**
* 设置字符集编码 * 设置字符集编码
* *

View File

@ -71,7 +71,7 @@ public class EnjoyEngine implements TemplateEngine {
if(null == this.engine){ if(null == this.engine){
init(TemplateConfig.DEFAULT); init(TemplateConfig.DEFAULT);
} }
if (ObjUtil.equal(ResourceMode.STRING, this.resourceMode)) { if (ObjUtil.equals(ResourceMode.STRING, this.resourceMode)) {
return EnjoyTemplate.wrap(this.engine.getTemplateByString(resource)); return EnjoyTemplate.wrap(this.engine.getTemplateByString(resource));
} }
return EnjoyTemplate.wrap(this.engine.getTemplate(resource)); return EnjoyTemplate.wrap(this.engine.getTemplate(resource));

View File

@ -66,4 +66,15 @@ public class MailTest {
final Properties props = account.getSmtpProps(); final Properties props = account.getSmtpProps();
Assert.assertEquals("true", props.getProperty("mail.debug")); Assert.assertEquals("true", props.getProperty("mail.debug"));
} }
@Test
@Ignore
public void sendHtmlWithPicsTest() {
HashMap<String, InputStream> map = new HashMap<>();
map.put("abc", FileUtil.getInputStream("D:/test/abc.png"));
map.put("abcd",FileUtil.getInputStream("D:/test/def.png"));
MailUtil.sendHtml("loolly@aliyun.com", "测试", "<h1>邮件来自Hutool测试</h1><img src=\"cid:abc\"/><img src=\"cid:abcd\"/>",
map);
}
} }