Browse Source

add img for mail

Looly 6 years ago
parent
commit
38dad619b8
2 changed files with 13 additions and 9 deletions
  1. 1 0
      CHANGELOG.md
  2. 12 9
      hutool-extra/src/main/java/cn/hutool/extra/mail/Mail.java

+ 1 - 0
CHANGELOG.md

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

+ 12 - 9
hutool-extra/src/main/java/cn/hutool/extra/mail/Mail.java

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