2525import java .io .IOException ;
2626import java .io .InputStream ;
2727import java .io .UnsupportedEncodingException ;
28- import java .util .ArrayList ;
29- import java .util .Arrays ;
30- import java .util .Collections ;
31- import java .util .HashMap ;
32- import java .util .Iterator ;
33- import java .util .List ;
34- import java .util .Map ;
35- import java .util .TreeMap ;
28+ import java .util .*;
3629
3730import static java .lang .String .format ;
3831import static org .simplejavamail .internal .util .MiscUtil .extractCID ;
@@ -133,13 +126,21 @@ private static void parseMimePartTree(@Nonnull final MimePart currentPart, @Nonn
133126 final DataSource ds = createDataSource (currentPart );
134127 // if the diposition is not provided, for now the part should be treated as inline (later non-embedded inline attachments are moved)
135128 if (Part .ATTACHMENT .equalsIgnoreCase (disposition )) {
136- parsedComponents .attachmentList .put (parseResourceName (parseContentID (currentPart ), parseFileName (currentPart )), ds );
129+ String resourceName = parseResourceName (parseContentID (currentPart ), parseFileName (currentPart ));
130+ if (valueNullOrEmpty (resourceName )) {
131+ resourceName = "unnamed" ;
132+ }
133+ parsedComponents .attachmentList .add (new AbstractMap .SimpleEntry (resourceName , ds ));
137134 } else if (disposition == null || Part .INLINE .equalsIgnoreCase (disposition )) {
138135 if (parseContentID (currentPart ) != null ) {
139136 parsedComponents .cidMap .put (parseContentID (currentPart ), ds );
140137 } else {
141138 // contentID missing -> treat as standard attachment
142- parsedComponents .attachmentList .put (parseResourceName (null , parseFileName (currentPart )), ds );
139+ String resourceName = parseResourceName (null , parseFileName (currentPart ));
140+ if (valueNullOrEmpty (resourceName )) {
141+ resourceName = "unnamed" ;
142+ }
143+ parsedComponents .attachmentList .add (new AbstractMap .SimpleEntry (resourceName , ds ));
143144 }
144145 } else {
145146 throw new IllegalStateException ("invalid attachment type" );
@@ -454,14 +455,14 @@ static void moveInvalidEmbeddedResourcesToAttachments(ParsedMimeMessageComponent
454455 Map .Entry <String , DataSource > cidEntry = it .next ();
455456 String cid = extractCID (cidEntry .getKey ());
456457 if (htmlContent == null || !htmlContent .contains ("cid:" + cid )) {
457- parsedComponents .attachmentList .put ( cid , cidEntry .getValue ());
458+ parsedComponents .attachmentList .add ( new AbstractMap . SimpleEntry ( cid , cidEntry .getValue () ));
458459 it .remove ();
459460 }
460461 }
461462 }
462463
463464 public static class ParsedMimeMessageComponents {
464- final Map <String , DataSource > attachmentList = new TreeMap <>();
465+ final List < Map . Entry <String , DataSource >> attachmentList = new ArrayList <>();
465466 final Map <String , DataSource > cidMap = new TreeMap <>();
466467 private final Map <String , Object > headers = new HashMap <>();
467468 private final List <InternetAddress > toAddresses = new ArrayList <>();
@@ -481,7 +482,7 @@ public String getMessageId() {
481482 return messageId ;
482483 }
483484
484- public Map <String , DataSource > getAttachmentList () {
485+ public List < Map . Entry <String , DataSource > > getAttachmentList () {
485486 return attachmentList ;
486487 }
487488
0 commit comments