68 lines
2.5 KiB
Java
68 lines
2.5 KiB
Java
package com.ruoyi.common.utils;
|
|||
|
|
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
|
||
|
|
import javax.mail.*;
|
||
|
|
import javax.mail.internet.MimeMessage;
|
||
|
|
import java.io.BufferedReader;
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.io.InputStream;
|
||
|
|
import java.io.InputStreamReader;
|
||
|
|
|
||
|
|
@Slf4j
|
||
|
|
public final class EmailUtil {
|
||
|
|
|
||
|
|
private static final String multipart = "multipart/*";
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public static String getMessageId(Part part, Session session) throws Exception {
|
||
|
|
if (!part.isMimeType(multipart)) {
|
||
|
|
return "";
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
Multipart multipart = (Multipart) part.getContent();
|
||
|
|
for (int i = 0; i < multipart.getCount(); i++) {
|
||
|
|
BodyPart bodyPart = multipart.getBodyPart(i);
|
||
|
|
if(bodyPart.getContentType().contains("Message/Rfc822")){
|
||
|
|
MimeMessage mimeMessage = new MimeMessage(session, bodyPart.getInputStream());
|
||
|
|
if(mimeMessage.getSubject()!=null&&mimeMessage.getSubject().contains("裁决书")){
|
||
|
|
String[] split = mimeMessage.getSubject().split("裁决书");
|
||
|
|
if(split.length>0){
|
||
|
|
return split[0];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} return "";
|
||
|
|
}
|
||
|
|
|
||
|
|
public static boolean isContainAttachment(Part part) {
|
||
|
|
boolean attachFlag = false;
|
||
|
|
try {
|
||
|
|
if (part.isMimeType(multipart)) {
|
||
|
|
Multipart mp = (Multipart) part.getContent();
|
||
|
|
for (int i = 0; i < mp.getCount(); i++) {
|
||
|
|
BodyPart mpart = mp.getBodyPart(i);
|
||
|
|
String disposition = mpart.getDisposition();
|
||
|
|
if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT)) || (disposition.equals(Part.INLINE))))
|
||
|
|
attachFlag = true;
|
||
|
|
else if (mpart.isMimeType(multipart)) {
|
||
|
|
attachFlag = isContainAttachment((Part) mpart);
|
||
|
|
} else {
|
||
|
|
String contype = mpart.getContentType();
|
||
|
|
if (contype.toLowerCase().contains("application"))
|
||
|
|
attachFlag = true;
|
||
|
|
if (contype.toLowerCase().contains("name"))
|
||
|
|
attachFlag = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (part.isMimeType("message/rfc822")) {
|
||
|
|
attachFlag = isContainAttachment((Part) part.getContent());
|
||
|
|
}
|
||
|
|
} catch (MessagingException | IOException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
return attachFlag;
|
||
|
|
}
|
||
|
|
}
|