|
@@ -7,6 +7,7 @@ import java.io.OutputStream;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URL;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
@@ -17,8 +18,12 @@ import javax.activation.DataSource;
|
|
|
import javax.activation.FileDataSource;
|
|
|
import javax.activation.FileTypeMap;
|
|
|
import javax.mail.Authenticator;
|
|
|
+import javax.mail.BodyPart;
|
|
|
+import javax.mail.Flags;
|
|
|
+import javax.mail.Message;
|
|
|
import javax.mail.MessagingException;
|
|
|
import javax.mail.Multipart;
|
|
|
+import javax.mail.Part;
|
|
|
import javax.mail.PasswordAuthentication;
|
|
|
import javax.mail.Session;
|
|
|
import javax.mail.internet.MimeBodyPart;
|
|
@@ -31,6 +36,7 @@ import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
|
import com.chelvc.framework.email.Body;
|
|
|
import com.chelvc.framework.email.config.EmailProperties;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
import lombok.NonNull;
|
|
|
|
|
|
/**
|
|
@@ -112,8 +118,8 @@ public final class EmailContextHolder {
|
|
|
}
|
|
|
|
|
|
// 关联文本内容里面的图片、链接
|
|
|
- Matcher matcher = StringUtils.getPattern(StringUtils.IMAGE_REGEX).matcher(text);
|
|
|
List<MimeBodyPart> relates = new LinkedList<>();
|
|
|
+ Matcher matcher = StringUtils.getPattern(StringUtils.IMAGE_REGEX).matcher(text);
|
|
|
while (matcher.find()) {
|
|
|
String img = matcher.group();
|
|
|
int i = img.indexOf("\"", img.indexOf(" src")) + 1;
|
|
@@ -173,6 +179,20 @@ public final class EmailContextHolder {
|
|
|
return part(file, file.getName());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将文件转换成邮件附件部分
|
|
|
+ *
|
|
|
+ * @param files 文件对象数组
|
|
|
+ * @return 邮件混合部分数组
|
|
|
+ */
|
|
|
+ public static MimeBodyPart[] part(@NonNull File... files) {
|
|
|
+ MimeBodyPart[] mixes = new MimeBodyPart[files.length];
|
|
|
+ for (int i = 0; i < files.length; i++) {
|
|
|
+ mixes[i] = part(files[i]);
|
|
|
+ }
|
|
|
+ return mixes;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 将文件转换成邮件附件部分
|
|
|
*
|
|
@@ -181,10 +201,21 @@ public final class EmailContextHolder {
|
|
|
* @return 邮件混合部分
|
|
|
*/
|
|
|
public static MimeBodyPart part(@NonNull File file, @NonNull String comment) {
|
|
|
+ return part(new FileDataSource(file), comment);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将文件转换成邮件附件部分
|
|
|
+ *
|
|
|
+ * @param source 数据源
|
|
|
+ * @param comment 文件说明
|
|
|
+ * @return 邮件混合部分
|
|
|
+ */
|
|
|
+ public static MimeBodyPart part(@NonNull DataSource source, @NonNull String comment) {
|
|
|
MimeBodyPart body = new MimeBodyPart();
|
|
|
try {
|
|
|
- body.setDataHandler(new DataHandler(new FileDataSource(file)));
|
|
|
body.setFileName(MimeUtility.encodeText(comment));
|
|
|
+ body.setDataHandler(new DataHandler(source));
|
|
|
} catch (MessagingException | UnsupportedEncodingException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
@@ -192,17 +223,15 @@ public final class EmailContextHolder {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 将文件转换成邮件附件部分
|
|
|
+ * 将文件流转换成邮件附件部分
|
|
|
*
|
|
|
- * @param files 文件对象数组
|
|
|
- * @return 邮件混合部分数组
|
|
|
+ * @param bytes 字节数组
|
|
|
+ * @param type 文件类型
|
|
|
+ * @param comment 文件说明
|
|
|
+ * @return 邮件混合部分
|
|
|
*/
|
|
|
- public static MimeBodyPart[] part(@NonNull File... files) {
|
|
|
- MimeBodyPart[] mixes = new MimeBodyPart[files.length];
|
|
|
- for (int i = 0; i < files.length; i++) {
|
|
|
- mixes[i] = part(files[i]);
|
|
|
- }
|
|
|
- return mixes;
|
|
|
+ public static MimeBodyPart part(@NonNull byte[] bytes, @NonNull String type, @NonNull String comment) {
|
|
|
+ return part(new ByteArrayDataSource(bytes, type), comment);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -214,16 +243,11 @@ public final class EmailContextHolder {
|
|
|
* @return 邮件混合部分
|
|
|
*/
|
|
|
public static MimeBodyPart part(@NonNull InputStream stream, @NonNull String type, @NonNull String comment) {
|
|
|
- MimeBodyPart body = new MimeBodyPart();
|
|
|
try {
|
|
|
- DataSource dataSource = new ByteArrayDataSource(stream, type);
|
|
|
- DataHandler dataHandler = new DataHandler(dataSource);
|
|
|
- body.setDataHandler(dataHandler);
|
|
|
- body.setFileName(MimeUtility.encodeText(comment));
|
|
|
- } catch (IOException | MessagingException e) {
|
|
|
+ return part(new ByteArrayDataSource(stream, type), comment);
|
|
|
+ } catch (IOException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
- return body;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -264,4 +288,64 @@ public final class EmailContextHolder {
|
|
|
}
|
|
|
return mixed;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置邮件删除标记
|
|
|
+ *
|
|
|
+ * @param message 邮件消息
|
|
|
+ */
|
|
|
+ public static void delete(@NonNull Message message) {
|
|
|
+ try {
|
|
|
+ message.setFlag(Flags.Flag.DELETED, true);
|
|
|
+ } catch (MessagingException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取附件文件名
|
|
|
+ *
|
|
|
+ * @param part 附件信息
|
|
|
+ * @return 文件名
|
|
|
+ */
|
|
|
+ public static String getFilename(@NonNull BodyPart part) {
|
|
|
+ try {
|
|
|
+ String filename = part.getFileName();
|
|
|
+ return StringUtils.isEmpty(filename) ? filename : MimeUtility.decodeText(filename);
|
|
|
+ } catch (MessagingException | UnsupportedEncodingException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取邮件附件
|
|
|
+ *
|
|
|
+ * @param message 邮件消息
|
|
|
+ * @return 附件列表
|
|
|
+ */
|
|
|
+ public static List<BodyPart> getAttachments(@NonNull Message message) {
|
|
|
+ try {
|
|
|
+ Object content = message.getContent();
|
|
|
+ if (!(content instanceof Multipart)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ Multipart multipart = (Multipart) content;
|
|
|
+ int count = multipart.getCount();
|
|
|
+ if (count == 0) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<BodyPart> parts = Lists.newLinkedList();
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ BodyPart part = multipart.getBodyPart(i);
|
|
|
+ if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
|
|
|
+ parts.add(part);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return parts.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(parts);
|
|
|
+ } catch (IOException | MessagingException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|