|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+package com.ruoyi.common.utils;
|
|
|
2
|
+
|
|
|
3
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
4
|
+
|
|
|
5
|
+import javax.mail.*;
|
|
|
6
|
+import javax.mail.internet.MimeMessage;
|
|
|
7
|
+import java.io.BufferedReader;
|
|
|
8
|
+import java.io.IOException;
|
|
|
9
|
+import java.io.InputStream;
|
|
|
10
|
+import java.io.InputStreamReader;
|
|
|
11
|
+
|
|
|
12
|
+@Slf4j
|
|
|
13
|
+public final class EmailUtil {
|
|
|
14
|
+
|
|
|
15
|
+ private static final String multipart = "multipart/*";
|
|
|
16
|
+
|
|
|
17
|
+
|
|
|
18
|
+
|
|
|
19
|
+ public static String getMessageId(Part part, Session session) throws Exception {
|
|
|
20
|
+ if (!part.isMimeType(multipart)) {
|
|
|
21
|
+ return "";
|
|
|
22
|
+
|
|
|
23
|
+ }
|
|
|
24
|
+
|
|
|
25
|
+ Multipart multipart = (Multipart) part.getContent();
|
|
|
26
|
+ for (int i = 0; i < multipart.getCount(); i++) {
|
|
|
27
|
+ BodyPart bodyPart = multipart.getBodyPart(i);
|
|
|
28
|
+ if(bodyPart.getContentType().contains("Message/Rfc822")){
|
|
|
29
|
+ MimeMessage mimeMessage = new MimeMessage(session, bodyPart.getInputStream());
|
|
|
30
|
+ if(mimeMessage.getSubject()!=null&&mimeMessage.getSubject().contains("裁决书")){
|
|
|
31
|
+ String[] split = mimeMessage.getSubject().split("裁决书");
|
|
|
32
|
+ if(split.length>0){
|
|
|
33
|
+ return split[0];
|
|
|
34
|
+ }
|
|
|
35
|
+ }
|
|
|
36
|
+ }
|
|
|
37
|
+ } return "";
|
|
|
38
|
+ }
|
|
|
39
|
+
|
|
|
40
|
+ public static boolean isContainAttachment(Part part) {
|
|
|
41
|
+ boolean attachFlag = false;
|
|
|
42
|
+ try {
|
|
|
43
|
+ if (part.isMimeType(multipart)) {
|
|
|
44
|
+ Multipart mp = (Multipart) part.getContent();
|
|
|
45
|
+ for (int i = 0; i < mp.getCount(); i++) {
|
|
|
46
|
+ BodyPart mpart = mp.getBodyPart(i);
|
|
|
47
|
+ String disposition = mpart.getDisposition();
|
|
|
48
|
+ if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT)) || (disposition.equals(Part.INLINE))))
|
|
|
49
|
+ attachFlag = true;
|
|
|
50
|
+ else if (mpart.isMimeType(multipart)) {
|
|
|
51
|
+ attachFlag = isContainAttachment((Part) mpart);
|
|
|
52
|
+ } else {
|
|
|
53
|
+ String contype = mpart.getContentType();
|
|
|
54
|
+ if (contype.toLowerCase().contains("application"))
|
|
|
55
|
+ attachFlag = true;
|
|
|
56
|
+ if (contype.toLowerCase().contains("name"))
|
|
|
57
|
+ attachFlag = true;
|
|
|
58
|
+ }
|
|
|
59
|
+ }
|
|
|
60
|
+ } else if (part.isMimeType("message/rfc822")) {
|
|
|
61
|
+ attachFlag = isContainAttachment((Part) part.getContent());
|
|
|
62
|
+ }
|
|
|
63
|
+ } catch (MessagingException | IOException e) {
|
|
|
64
|
+ e.printStackTrace();
|
|
|
65
|
+ }
|
|
|
66
|
+ return attachFlag;
|
|
|
67
|
+ }
|
|
|
68
|
+}
|