mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add img for mail
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
### 新特性
|
||||
* 【core】 改进CollUtil.zip逻辑,减少内存复制(issue#I10T01@Gitee)
|
||||
* 【extra】 邮件支持图片(pr#495@Github)
|
||||
|
||||
### Bug修复
|
||||
* 【http】 修复HttpRquest中body方法长度计算问题(issue#I10UPG@Gitee)
|
||||
|
@@ -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;
|
||||
|
||||
@@ -192,7 +193,7 @@ public class Mail {
|
||||
* @return this
|
||||
*/
|
||||
public Mail setFiles(File... files) {
|
||||
if(ArrayUtil.isEmpty(files)) {
|
||||
if (ArrayUtil.isEmpty(files)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
|
Reference in New Issue
Block a user