-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathMimeMessageParser.java
523 lines (465 loc) · 18.3 KB
/
MimeMessageParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package org.simplejavamail.converter.internal.mimemessage;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.mail.Address;
import javax.mail.Header;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.AddressException;
import javax.mail.internet.ContentType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimePart;
import javax.mail.internet.MimeUtility;
import javax.mail.internet.ParseException;
import javax.mail.util.ByteArrayDataSource;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.lang.String.format;
import static org.simplejavamail.internal.util.MiscUtil.valueNullOrEmpty;
/**
* Parses a MimeMessage and stores the individual parts such a plain text, HTML text and attachments.
*
* @version current: MimeMessageParser.java 2016-02-25 Benny Bottema
*/
public final class MimeMessageParser {
/**
* Contains the headers we will ignore, because either we set the information differently (such as Subject) or we recognize the header as
* interfering or obsolete for new emails).
*/
private static final List<String> HEADERS_TO_IGNORE = new ArrayList<>();
static {
// taken from: protected javax.mail.internet.InternetHeaders constructor
/*
* When extracting information to create an Email, we're NOT interested in the following headers:
*/
// HEADERS_TO_IGNORE.add("Return-Path"); // bounceTo address
HEADERS_TO_IGNORE.add("Received");
HEADERS_TO_IGNORE.add("Resent-Date");
HEADERS_TO_IGNORE.add("Resent-From");
HEADERS_TO_IGNORE.add("Resent-Sender");
HEADERS_TO_IGNORE.add("Resent-To");
HEADERS_TO_IGNORE.add("Resent-Cc");
HEADERS_TO_IGNORE.add("Resent-Bcc");
HEADERS_TO_IGNORE.add("Resent-Message-Id");
HEADERS_TO_IGNORE.add("Date");
HEADERS_TO_IGNORE.add("From");
HEADERS_TO_IGNORE.add("Sender");
HEADERS_TO_IGNORE.add("Reply-To");
HEADERS_TO_IGNORE.add("To");
HEADERS_TO_IGNORE.add("Cc");
HEADERS_TO_IGNORE.add("Bcc");
HEADERS_TO_IGNORE.add("Message-Id");
// The next two are needed for replying to
// HEADERS_TO_IGNORE.add("In-Reply-To");
// HEADERS_TO_IGNORE.add("References");
HEADERS_TO_IGNORE.add("Subject");
HEADERS_TO_IGNORE.add("Comments");
HEADERS_TO_IGNORE.add("Keywords");
HEADERS_TO_IGNORE.add("Errors-To");
HEADERS_TO_IGNORE.add("MIME-Version");
HEADERS_TO_IGNORE.add("Content-Type");
HEADERS_TO_IGNORE.add("Content-Transfer-Encoding");
HEADERS_TO_IGNORE.add("Content-MD5");
HEADERS_TO_IGNORE.add(":");
HEADERS_TO_IGNORE.add("Content-Length");
HEADERS_TO_IGNORE.add("Status");
// extra headers that should be ignored, which may originate from nested attachments
HEADERS_TO_IGNORE.add("Content-Disposition");
HEADERS_TO_IGNORE.add("size");
HEADERS_TO_IGNORE.add("filename");
HEADERS_TO_IGNORE.add("Content-ID");
HEADERS_TO_IGNORE.add("name");
HEADERS_TO_IGNORE.add("From");
}
/**
* Extracts the content of a MimeMessage recursively.
*/
public static ParsedMimeMessageComponents parseMimeMessage(@Nonnull final MimeMessage mimeMessage) {
final ParsedMimeMessageComponents parsedComponents = new ParsedMimeMessageComponents();
parsedComponents.messageId = parseMessageId(mimeMessage);
parsedComponents.subject = parseSubject(mimeMessage);
parsedComponents.toAddresses.addAll(parseToAddresses(mimeMessage));
parsedComponents.ccAddresses.addAll(parseCcAddresses(mimeMessage));
parsedComponents.bccAddresses.addAll(parseBccAddresses(mimeMessage));
parsedComponents.fromAddress = parseFromAddress(mimeMessage);
parsedComponents.replyToAddresses = parseReplyToAddresses(mimeMessage);
parseMimePartTree(mimeMessage, parsedComponents);
return parsedComponents;
}
private static void parseMimePartTree(@Nonnull final MimePart currentPart, @Nonnull final ParsedMimeMessageComponents parsedComponents) {
for (final Header header : retrieveAllHeaders(currentPart)) {
parseHeader(header, parsedComponents);
}
final String disposition = parseDisposition(currentPart);
if (isMimeType(currentPart, "text/plain") && parsedComponents.plainContent == null && !Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
parsedComponents.plainContent = parseContent(currentPart);
} else if (isMimeType(currentPart, "text/html") && parsedComponents.htmlContent == null && !Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
parsedComponents.htmlContent = parseContent(currentPart);
} else if (isMimeType(currentPart, "multipart/*")) {
final Multipart mp = parseContent(currentPart);
for (int i = 0, count = countBodyParts(mp); i < count; i++) {
parseMimePartTree(getBodyPartAtIndex(mp, i), parsedComponents);
}
} else {
final DataSource ds = createDataSource(currentPart);
// If the diposition is not provided, the part should be treated as attachment
if (disposition == null || Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
parsedComponents.attachmentList.put(parseResourceName(parseContentID(currentPart), parseFileName(currentPart)), ds);
} else if (Part.INLINE.equalsIgnoreCase(disposition)) {
if (parseContentID(currentPart) != null) {
parsedComponents.cidMap.put(parseContentID(currentPart), ds);
} else {
// contentID missing -> treat as standard attachment
parsedComponents.attachmentList.put(parseResourceName(null, parseFileName(currentPart)), ds);
}
} else {
throw new IllegalStateException("invalid attachment type");
}
}
}
@SuppressWarnings("StatementWithEmptyBody")
private static void parseHeader(final Header header, @Nonnull final ParsedMimeMessageComponents parsedComponents) {
if (header.getName().equals("Disposition-Notification-To")) {
parsedComponents.dispositionNotificationTo = createAddress(header, "Disposition-Notification-To");
} else if (header.getName().equals("Return-Receipt-To")) {
parsedComponents.returnReceiptTo = createAddress(header, "Return-Receipt-To");
} else if (header.getName().equals("Return-Path")) {
parsedComponents.bounceToAddress = createAddress(header, "Return-Path");
} else if (!HEADERS_TO_IGNORE.contains(header.getName())) {
parsedComponents.headers.put(header.getName(), header.getValue());
} else {
// header recognized, but not relevant (see #HEADERS_TO_IGNORE)
}
}
@SuppressWarnings("WeakerAccess")
public static String parseFileName(@Nonnull final Part currentPart) {
try {
return currentPart.getFileName();
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_GETTING_FILENAME, e);
}
}
@SuppressWarnings("WeakerAccess")
@Nullable
public static String parseContentID(@Nonnull final MimePart currentPart) {
try {
return currentPart.getContentID();
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_GETTING_CONTENT_ID, e);
}
}
@SuppressWarnings("WeakerAccess")
public static MimeBodyPart getBodyPartAtIndex(final Multipart parentMultiPart, final int index) {
try {
return (MimeBodyPart) parentMultiPart.getBodyPart(index);
} catch (final MessagingException e) {
throw new MimeMessageParseException(format(MimeMessageParseException.ERROR_GETTING_BODYPART_AT_INDEX, index), e);
}
}
@SuppressWarnings("WeakerAccess")
public static int countBodyParts(final Multipart mp) {
try {
return mp.getCount();
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_PARSING_MULTIPART_COUNT, e);
}
}
@SuppressWarnings("WeakerAccess")
public static <T> T parseContent(@Nonnull final MimePart currentPart) {
try {
//noinspection unchecked
return (T) currentPart.getContent();
} catch (IOException | MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_PARSING_CONTENT, e);
}
}
@SuppressWarnings("WeakerAccess")
public static String parseDisposition(@Nonnull final MimePart currentPart) {
try {
return currentPart.getDisposition();
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_PARSING_DISPOSITION, e);
}
}
@Nonnull
private static String parseResourceName(@Nullable final String contentID, @Nonnull final String fileName) {
String extension = "";
if (!valueNullOrEmpty(fileName) && fileName.contains(".")) {
extension = fileName.substring(fileName.lastIndexOf("."), fileName.length());
}
if (!valueNullOrEmpty(contentID)) {
return (contentID.endsWith(extension)) ? contentID : contentID + extension;
} else {
return fileName;
}
}
@SuppressWarnings("WeakerAccess")
@Nonnull
public static List<Header> retrieveAllHeaders(@Nonnull final MimePart part) {
try {
return Collections.list(part.getAllHeaders());
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_GETTING_ALL_HEADERS, e);
}
}
@Nonnull
private static InternetAddress createAddress(final Header header, final String typeOfAddress) {
try {
return new InternetAddress(header.getValue());
} catch (final AddressException e) {
throw new MimeMessageParseException(format(MimeMessageParseException.ERROR_PARSING_ADDRESS, typeOfAddress), e);
}
}
/**
* Checks whether the MimePart contains an object of the given mime type.
*
* @param part the current MimePart
* @param mimeType the mime type to check
* @return {@code true} if the MimePart matches the given mime type, {@code false} otherwise
*/
@SuppressWarnings("WeakerAccess")
public static boolean isMimeType(@Nonnull final MimePart part, @Nonnull final String mimeType) {
// Do not use part.isMimeType(String) as it is broken for MimeBodyPart
// and does not really check the actual content type.
try {
final ContentType contentType = new ContentType(retrieveDataHandler(part).getContentType());
return contentType.match(mimeType);
} catch (final ParseException ex) {
return retrieveContentType(part).equalsIgnoreCase(mimeType);
}
}
@SuppressWarnings("WeakerAccess")
public static String retrieveContentType(@Nonnull final MimePart part) {
try {
return part.getContentType();
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_GETTING_CONTENT_TYPE, e);
}
}
@SuppressWarnings("WeakerAccess")
public static DataHandler retrieveDataHandler(@Nonnull final MimePart part) {
try {
return part.getDataHandler();
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_GETTING_DATAHANDLER, e);
}
}
/**
* Parses the MimePart to create a DataSource.
*
* @param part the current part to be processed
* @return the DataSource
*/
@Nonnull
private static DataSource createDataSource(@Nonnull final MimePart part) {
final DataHandler dataHandler = retrieveDataHandler(part);
final DataSource dataSource = dataHandler.getDataSource();
final String contentType = parseBaseMimeType(dataSource.getContentType());
final byte[] content = readContent(retrieveInputStream(dataSource));
final ByteArrayDataSource result = new ByteArrayDataSource(content, contentType);
final String dataSourceName = parseDataSourceName(part, dataSource);
result.setName(dataSourceName);
return result;
}
@SuppressWarnings("WeakerAccess")
public static InputStream retrieveInputStream(final DataSource dataSource) {
try {
return dataSource.getInputStream();
} catch (final IOException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_GETTING_INPUTSTREAM, e);
}
}
@Nullable
private static String parseDataSourceName(@Nonnull final Part part, @Nonnull final DataSource dataSource) {
final String result = !valueNullOrEmpty(dataSource.getName()) ? dataSource.getName() : parseFileName(part);
return !valueNullOrEmpty(result) ? decodeText(result) : null;
}
@Nonnull
private static String decodeText(@Nonnull final String result) {
try {
return MimeUtility.decodeText(result);
} catch (final UnsupportedEncodingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_DECODING_TEXT, e);
}
}
@Nonnull
private static byte[] readContent(@Nonnull final InputStream is) {
final BufferedInputStream isReader = new BufferedInputStream(is);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final BufferedOutputStream osWriter = new BufferedOutputStream(os);
int ch;
try {
while ((ch = isReader.read()) != -1) {
osWriter.write(ch);
}
osWriter.flush();
final byte[] result = os.toByteArray();
osWriter.close();
return result;
} catch (final IOException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_READING_CONTENT, e);
}
}
/**
* @param fullMimeType the mime type from the mail api
* @return The real mime type
*/
@Nonnull
private static String parseBaseMimeType(@Nonnull final String fullMimeType) {
final int pos = fullMimeType.indexOf(';');
if (pos >= 0) {
return fullMimeType.substring(0, pos);
}
return fullMimeType;
}
@SuppressWarnings("WeakerAccess")
@Nonnull
public static List<InternetAddress> parseToAddresses(@Nonnull final MimeMessage mimeMessage) {
return parseInternetAddresses(retrieveRecipients(mimeMessage, RecipientType.TO));
}
@SuppressWarnings("WeakerAccess")
@Nonnull
public static List<InternetAddress> parseCcAddresses(@Nonnull final MimeMessage mimeMessage) {
return parseInternetAddresses(retrieveRecipients(mimeMessage, RecipientType.CC));
}
@SuppressWarnings("WeakerAccess")
@Nonnull
public static List<InternetAddress> parseBccAddresses(@Nonnull final MimeMessage mimeMessage) {
return parseInternetAddresses(retrieveRecipients(mimeMessage, RecipientType.BCC));
}
@SuppressWarnings("WeakerAccess")
@Nullable
public static Address[] retrieveRecipients(@Nonnull final MimeMessage mimeMessage, final RecipientType recipientType) {
try {
return mimeMessage.getRecipients(recipientType);
} catch (final MessagingException e) {
throw new MimeMessageParseException(format(MimeMessageParseException.ERROR_GETTING_RECIPIENTS, recipientType), e);
}
}
@Nonnull
private static List<InternetAddress> parseInternetAddresses(@Nullable final Address[] recipients) {
final List<Address> addresses = (recipients != null) ? Arrays.asList(recipients) : new ArrayList<Address>();
final List<InternetAddress> mailAddresses = new ArrayList<>();
for (final Address address : addresses) {
if (address instanceof InternetAddress) {
mailAddresses.add((InternetAddress) address);
}
}
return mailAddresses;
}
@SuppressWarnings("WeakerAccess")
@Nullable
public static InternetAddress parseFromAddress(@Nonnull final MimeMessage mimeMessage) {
try {
final Address[] addresses = mimeMessage.getFrom();
return (addresses == null || addresses.length == 0) ? null : (InternetAddress) addresses[0];
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_PARSING_FROMADDRESS, e);
}
}
@SuppressWarnings("WeakerAccess")
@Nullable
public static InternetAddress parseReplyToAddresses(@Nonnull final MimeMessage mimeMessage) {
try {
final Address[] addresses = mimeMessage.getReplyTo();
return (addresses == null || addresses.length == 0) ? null : (InternetAddress) addresses[0];
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_PARSING_REPLY_TO_ADDRESSES, e);
}
}
@Nullable
public static String parseSubject(@Nonnull final MimeMessage mimeMessage) {
try {
return mimeMessage.getSubject();
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_GETTING_SUBJECT, e);
}
}
@SuppressWarnings("WeakerAccess")
@Nullable
public static String parseMessageId(@Nonnull final MimeMessage mimeMessage) {
try {
return mimeMessage.getMessageID();
} catch (final MessagingException e) {
throw new MimeMessageParseException(MimeMessageParseException.ERROR_GETTING_MESSAGE_ID, e);
}
}
public static class ParsedMimeMessageComponents {
private final Map<String, DataSource> attachmentList = new HashMap<>();
private final Map<String, DataSource> cidMap = new HashMap<>();
private final Map<String, Object> headers = new HashMap<>();
private final List<InternetAddress> toAddresses = new ArrayList<>();
private final List<InternetAddress> ccAddresses = new ArrayList<>();
private final List<InternetAddress> bccAddresses = new ArrayList<>();
private String messageId;
private String subject;
private InternetAddress fromAddress;
private InternetAddress replyToAddresses;
private InternetAddress dispositionNotificationTo;
private InternetAddress returnReceiptTo;
private InternetAddress bounceToAddress;
private String plainContent;
private String htmlContent;
public String getMessageId() {
return messageId;
}
public Map<String, DataSource> getAttachmentList() {
return attachmentList;
}
public Map<String, DataSource> getCidMap() {
return cidMap;
}
public Map<String, Object> getHeaders() {
return headers;
}
public List<InternetAddress> getToAddresses() {
return toAddresses;
}
public List<InternetAddress> getCcAddresses() {
return ccAddresses;
}
public List<InternetAddress> getBccAddresses() {
return bccAddresses;
}
public String getSubject() {
return subject;
}
public InternetAddress getFromAddress() {
return fromAddress;
}
public InternetAddress getReplyToAddresses() {
return replyToAddresses;
}
public InternetAddress getDispositionNotificationTo() {
return dispositionNotificationTo;
}
public InternetAddress getReturnReceiptTo() {
return returnReceiptTo;
}
public InternetAddress getBounceToAddress() {
return bounceToAddress;
}
public String getPlainContent() {
return plainContent;
}
public String getHtmlContent() {
return htmlContent;
}
}
}