11package org .simplejavamail .converter .internal .mimemessage ;
22
3+ import jakarta .activation .CommandMap ;
4+ import jakarta .activation .MailcapCommandMap ;
5+ import lombok .Getter ;
36import org .eclipse .angus .mail .handlers .text_plain ;
47import jakarta .activation .ActivationDataFlavor ;
5- import jakarta .activation .CommandMap ;
68import jakarta .activation .DataHandler ;
79import jakarta .activation .DataSource ;
8- import jakarta .activation .MailcapCommandMap ;
910import jakarta .mail .Address ;
1011import jakarta .mail .Message .RecipientType ;
1112import jakarta .mail .MessagingException ;
2324import lombok .val ;
2425import org .jetbrains .annotations .NotNull ;
2526import org .jetbrains .annotations .Nullable ;
27+ import org .simplejavamail .api .internal .general .HeadersToIgnoreWhenParsingExternalEmails ;
2628import org .simplejavamail .internal .util .MiscUtil ;
2729import org .simplejavamail .internal .util .NamedDataSource ;
2830import org .simplejavamail .internal .util .Preconditions ;
6062 */
6163public final class MimeMessageParser {
6264
63- /**
64- * Contains the headers we will ignore, because either we set the information differently (such as Subject) or we recognize the header as
65- * interfering or obsolete for new emails).
66- */
67- private static final List <String > HEADERS_TO_IGNORE = new ArrayList <>();
68-
6965 static {
70- // taken from: protected jakarta.mail.internet.InternetHeaders constructor
71- /*
72- * When extracting information to create an Email, we're NOT interested in the following headers:
73- */
74- // HEADERS_TO_IGNORE.add("Return-Path"); // bounceTo address
75- HEADERS_TO_IGNORE .add ("Received" );
76- HEADERS_TO_IGNORE .add ("Resent-Date" );
77- HEADERS_TO_IGNORE .add ("Resent-From" );
78- HEADERS_TO_IGNORE .add ("Resent-Sender" );
79- HEADERS_TO_IGNORE .add ("Resent-To" );
80- HEADERS_TO_IGNORE .add ("Resent-Cc" );
81- HEADERS_TO_IGNORE .add ("Resent-Bcc" );
82- HEADERS_TO_IGNORE .add ("Resent-Message-Id" );
83- HEADERS_TO_IGNORE .add ("Date" );
84- HEADERS_TO_IGNORE .add ("From" );
85- HEADERS_TO_IGNORE .add ("Sender" );
86- HEADERS_TO_IGNORE .add ("Reply-To" );
87- HEADERS_TO_IGNORE .add ("To" );
88- HEADERS_TO_IGNORE .add ("Cc" );
89- HEADERS_TO_IGNORE .add ("Bcc" );
90- HEADERS_TO_IGNORE .add ("Message-Id" );
91- // The next two are needed for replying to
92- // HEADERS_TO_IGNORE.add("In-Reply-To");
93- // HEADERS_TO_IGNORE.add("References");
94- HEADERS_TO_IGNORE .add ("Subject" );
95- HEADERS_TO_IGNORE .add ("Comments" );
96- HEADERS_TO_IGNORE .add ("Keywords" );
97- HEADERS_TO_IGNORE .add ("Errors-To" );
98- HEADERS_TO_IGNORE .add ("MIME-Version" );
99- HEADERS_TO_IGNORE .add ("Content-Type" );
100- HEADERS_TO_IGNORE .add ("Content-Transfer-Encoding" );
101- HEADERS_TO_IGNORE .add ("Content-MD5" );
102- HEADERS_TO_IGNORE .add (":" );
103- HEADERS_TO_IGNORE .add ("Content-Length" );
104- HEADERS_TO_IGNORE .add ("Status" );
105- // extra headers that should be ignored, which may originate from nested attachments
106- HEADERS_TO_IGNORE .add ("Content-Disposition" );
107- HEADERS_TO_IGNORE .add ("size" );
108- HEADERS_TO_IGNORE .add ("filename" );
109- HEADERS_TO_IGNORE .add ("Content-ID" );
110- HEADERS_TO_IGNORE .add ("name" );
111- HEADERS_TO_IGNORE .add ("From" );
112-
113- MailcapCommandMap mc = (MailcapCommandMap )CommandMap .getDefaultCommandMap ();
66+ MailcapCommandMap mc = (MailcapCommandMap ) CommandMap .getDefaultCommandMap ();
11467 mc .addMailcap ("text/calendar;; x-java-content-handler=" + text_calendar .class .getName ());
11568 CommandMap .setDefaultCommandMap (mc );
11669 }
@@ -210,7 +163,7 @@ private static void parseHeader(final DecodedHeader header, @NotNull final Parse
210163 parsedComponents .returnReceiptTo = createAddress (headerValue , "Return-Receipt-To" );
211164 } else if (isEmailHeader (header , "Return-Path" )) {
212165 parsedComponents .bounceToAddress = createAddress (headerValue , "Return-Path" );
213- } else if (!HEADERS_TO_IGNORE . contains (headerName )) {
166+ } else if (!HeadersToIgnoreWhenParsingExternalEmails . shouldIgnoreHeader (headerName )) {
214167 if (!parsedComponents .headers .containsKey (headerName )) {
215168 parsedComponents .headers .put (headerName , new ArrayList <>());
216169 }
@@ -439,8 +392,7 @@ private static String parseDataSourceName(@NotNull final Part part, @NotNull fin
439392 return !valueNullOrEmpty (result ) ? decodeText (result ) : null ;
440393 }
441394
442- @ NotNull
443- private static byte [] readContent (@ NotNull final InputStream is ) {
395+ private static byte @ NotNull [] readContent (@ NotNull final InputStream is ) {
444396 try {
445397 return MiscUtil .readInputStreamToBytes (is );
446398 } catch (final IOException e ) {
@@ -606,86 +558,27 @@ static void moveInvalidEmbeddedResourcesToAttachments(ParsedMimeMessageComponent
606558 }
607559 }
608560
561+ @ Getter
609562 public static class ParsedMimeMessageComponents {
610- @ SuppressWarnings ("unchecked" )
611563 final Set <MimeDataSource > attachmentList = new TreeSet <>();
612564 final Map <String , DataSource > cidMap = new TreeMap <>();
613565 private final Map <String , Collection <Object >> headers = new HashMap <>();
614566 private final List <InternetAddress > toAddresses = new ArrayList <>();
615567 private final List <InternetAddress > ccAddresses = new ArrayList <>();
616568 private final List <InternetAddress > bccAddresses = new ArrayList <>();
617- private String messageId ;
618- private String subject ;
619- private InternetAddress fromAddress ;
620- private InternetAddress replyToAddresses ;
621- private InternetAddress dispositionNotificationTo ;
622- private InternetAddress returnReceiptTo ;
623- private InternetAddress bounceToAddress ;
624- private String contentTransferEncoding ;
569+ @ Nullable private String messageId ;
570+ @ Nullable private String subject ;
571+ @ Nullable private InternetAddress fromAddress ;
572+ @ Nullable private InternetAddress replyToAddresses ;
573+ @ Nullable private InternetAddress dispositionNotificationTo ;
574+ @ Nullable private InternetAddress returnReceiptTo ;
575+ @ Nullable private InternetAddress bounceToAddress ;
576+ @ Nullable private String contentTransferEncoding ;
625577 private final StringBuilder plainContent = new StringBuilder ();
626578 final StringBuilder htmlContent = new StringBuilder ();
627- private String calendarMethod ;
628- private String calendarContent ;
629- private Date sentDate ;
630-
631- @ Nullable
632- public String getMessageId () {
633- return messageId ;
634- }
635-
636- public Set <MimeDataSource > getAttachmentList () {
637- return attachmentList ;
638- }
639-
640- public Map <String , DataSource > getCidMap () {
641- return cidMap ;
642- }
643-
644- public Map <String , Collection <Object >> getHeaders () {
645- return headers ;
646- }
647-
648- public List <InternetAddress > getToAddresses () {
649- return toAddresses ;
650- }
651-
652- public List <InternetAddress > getCcAddresses () {
653- return ccAddresses ;
654- }
655-
656- public List <InternetAddress > getBccAddresses () {
657- return bccAddresses ;
658- }
659-
660- @ Nullable
661- public String getSubject () {
662- return subject ;
663- }
664-
665- @ Nullable
666- public InternetAddress getFromAddress () {
667- return fromAddress ;
668- }
669-
670- @ Nullable
671- public InternetAddress getReplyToAddresses () {
672- return replyToAddresses ;
673- }
674-
675- @ Nullable
676- public InternetAddress getDispositionNotificationTo () {
677- return dispositionNotificationTo ;
678- }
679-
680- @ Nullable
681- public InternetAddress getReturnReceiptTo () {
682- return returnReceiptTo ;
683- }
684-
685- @ Nullable
686- public InternetAddress getBounceToAddress () {
687- return bounceToAddress ;
688- }
579+ @ Nullable private String calendarMethod ;
580+ @ Nullable private String calendarContent ;
581+ @ Nullable private Date sentDate ;
689582
690583 @ Nullable
691584 public String getPlainContent () {
@@ -697,21 +590,6 @@ public String getHtmlContent() {
697590 return htmlContent .length () == 0 ? null : htmlContent .toString ();
698591 }
699592
700- @ Nullable
701- public String getCalendarContent () {
702- return calendarContent ;
703- }
704-
705- @ Nullable
706- public String getContentTransferEncoding () {
707- return contentTransferEncoding ;
708- }
709-
710- @ Nullable
711- public String getCalendarMethod () {
712- return calendarMethod ;
713- }
714-
715593 @ Nullable
716594 public Date getSentDate () {
717595 return sentDate != null ? new Date (sentDate .getTime ()) : null ;
0 commit comments