Skip to content

Commit a675673

Browse files
author
Nicolas Bélisle
committed
https://github.com/bbottema/simple-java-mail/issues/219
1 parent df377c5 commit a675673

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

src/main/java/org/simplejavamail/converter/EmailConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ private static void buildEmailFromMimeMessage(@Nonnull final EmailPopulatingBuil
283283
final String cidName = checkNonEmptyArgument(cid.getKey(), "cid.key");
284284
builder.withEmbeddedImage(extractCID(cidName), cid.getValue());
285285
}
286-
for (final Map.Entry<String, DataSource> attachment : parsed.getAttachmentList().entrySet()) {
286+
for (final Map.Entry<String, DataSource> attachment : parsed.getAttachmentList()) {
287287
builder.withAttachment(extractCID(attachment.getKey()), attachment.getValue());
288288
}
289289
}

src/main/java/org/simplejavamail/converter/internal/mimemessage/MimeMessageParser.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import 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

3730
import static java.lang.String.format;
3831
import 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

src/test/java/org/simplejavamail/converter/internal/mimemessage/MimeMessageParserTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public void testMoveInvalidEmbeddedResourcesToAttachments_NoHtmlNoInvalid() thro
1818
moveInvalidEmbeddedResourcesToAttachments(parsedComponents);
1919

2020
assertThat(parsedComponents.cidMap).isEmpty();
21-
assertThat(parsedComponents.attachmentList).containsOnlyKeys("moo1","moo2");
21+
assertThat(parsedComponents.attachmentList.size()).isEqualTo(2);
22+
assertThat(parsedComponents.attachmentList.get(0).getKey()).isEqualTo("moo1");
23+
assertThat(parsedComponents.attachmentList.get(1).getKey()).isEqualTo("moo2");
2224
}
2325
@Test
2426
public void testMoveInvalidEmbeddedResourcesToAttachments_HtmlButNoInvalid() throws IOException {
@@ -29,7 +31,9 @@ public void testMoveInvalidEmbeddedResourcesToAttachments_HtmlButNoInvalid() thr
2931
moveInvalidEmbeddedResourcesToAttachments(parsedComponents);
3032

3133
assertThat(parsedComponents.cidMap).isEmpty();
32-
assertThat(parsedComponents.attachmentList).containsOnlyKeys("moo1","moo2");
34+
assertThat(parsedComponents.attachmentList.size()).isEqualTo(2);
35+
assertThat(parsedComponents.attachmentList.get(0).getKey()).isEqualTo("moo1");
36+
assertThat(parsedComponents.attachmentList.get(1).getKey()).isEqualTo("moo2");
3337
}
3438

3539
@Test
@@ -41,6 +45,7 @@ public void testMoveInvalidEmbeddedResourcesToAttachments_Invalid() throws IOExc
4145
moveInvalidEmbeddedResourcesToAttachments(parsedComponents);
4246

4347
assertThat(parsedComponents.cidMap).containsOnlyKeys("moo1");
44-
assertThat(parsedComponents.attachmentList).containsOnlyKeys("moo2");
48+
assertThat(parsedComponents.attachmentList.size()).isEqualTo(1);
49+
assertThat(parsedComponents.attachmentList.get(0).getKey()).isEqualTo("moo2");
4550
}
4651
}

0 commit comments

Comments
 (0)