add img for mail

This commit is contained in:
Looly
2019-08-20 11:45:52 +08:00
parent ef6214a303
commit 38dad619b8
2 changed files with 13 additions and 9 deletions

View File

@@ -7,6 +7,7 @@
### 新特性 ### 新特性
* 【core】 改进CollUtil.zip逻辑减少内存复制issue#I10T01@Gitee * 【core】 改进CollUtil.zip逻辑减少内存复制issue#I10T01@Gitee
* 【extra】 邮件支持图片pr#495@Github
### Bug修复 ### Bug修复
* 【http】 修复HttpRquest中body方法长度计算问题issue#I10UPG@Gitee * 【http】 修复HttpRquest中body方法长度计算问题issue#I10UPG@Gitee

View File

@@ -21,6 +21,7 @@ import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource; import javax.mail.util.ByteArrayDataSource;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
@@ -139,7 +140,7 @@ public class Mail {
this.bccs = bccs; this.bccs = bccs;
return this; return this;
} }
/** /**
* 设置多个回复地址(reply-to) * 设置多个回复地址(reply-to)
* *
@@ -192,10 +193,10 @@ public class Mail {
* @return this * @return this
*/ */
public Mail setFiles(File... files) { public Mail setFiles(File... files) {
if(ArrayUtil.isEmpty(files)) { if (ArrayUtil.isEmpty(files)) {
return this; return this;
} }
final DataSource[] attachments = new DataSource[files.length]; final DataSource[] attachments = new DataSource[files.length];
for (int i = 0; i < files.length; i++) { for (int i = 0; i < files.length; i++) {
attachments[i] = new FileDataSource(files[i]); attachments[i] = new FileDataSource(files[i]);
@@ -211,7 +212,7 @@ public class Mail {
* @since 4.0.9 * @since 4.0.9
*/ */
public Mail setAttachments(DataSource... attachments) { public Mail setAttachments(DataSource... attachments) {
if(ArrayUtil.isNotEmpty(attachments)) { if (ArrayUtil.isNotEmpty(attachments)) {
this.attachments = attachments; this.attachments = attachments;
} }
return this; return this;
@@ -317,7 +318,7 @@ public class Mail {
if (ArrayUtil.isNotEmpty(this.reply)) { if (ArrayUtil.isNotEmpty(this.reply)) {
msg.setReplyTo(InternalMailUtil.parseAddressFromStrs(this.reply, charset)); msg.setReplyTo(InternalMailUtil.parseAddressFromStrs(this.reply, charset));
} }
return msg; return msg;
} }
@@ -349,18 +350,20 @@ public class Mail {
// 图片 // 图片
for (Map.Entry<String, InputStream> entry : imageMap.entrySet()) { for (Map.Entry<String, InputStream> entry : imageMap.entrySet()) {
BodyPart messageBodyPart = new MimeBodyPart(); MimeBodyPart imgBodyPart = new MimeBodyPart();
DataSource ds; DataSource ds;
try { try {
ds = new ByteArrayDataSource(entry.getValue(), "image/jpeg"); ds = new ByteArrayDataSource(entry.getValue(), "image/jpeg");
IoUtil.close(entry.getValue());
} catch (IOException e) { } catch (IOException e) {
throw new MailException(e); throw new MailException(e);
} }
messageBodyPart.setDataHandler(new DataHandler(ds)); imgBodyPart.setDataHandler(new DataHandler(ds));
messageBodyPart.setHeader("Content-ID", String.format("<%s>", entry.getKey())); // imgBodyPart.setHeader("Content-ID", String.format("<%s>", entry.getKey()));
imgBodyPart.setContentID(entry.getKey());
// add it // add it
mainPart.addBodyPart(messageBodyPart); mainPart.addBodyPart(imgBodyPart);
} }
return mainPart; return mainPart;