Skip to content

Commit 96d758e

Browse files
committed
#444 improve decoding of recipient addresses, properly handling delimitation
1 parent 5425c66 commit 96d758e

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

modules/simple-java-mail/src/main/java/org/simplejavamail/converter/internal/mimemessage/MimeMessageParser.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.regex.Matcher;
4646
import java.util.regex.Pattern;
4747

48+
import static com.pivovarit.function.ThrowingFunction.unchecked;
4849
import static java.lang.String.format;
4950
import static java.nio.charset.StandardCharsets.UTF_8;
5051
import static java.util.Optional.ofNullable;
@@ -471,10 +472,14 @@ public static Address[] retrieveRecipients(@NotNull final MimeMessage mimeMessag
471472
try {
472473
// return mimeMessage.getRecipients(recipientType); // can fail in strict mode, see https://github.com/bbottema/simple-java-mail/issues/227
473474
// workaround following (copied and modified from JavaMail internal code):
474-
val s = ofNullable(mimeMessage.getHeader(getHeaderName(recipientType), ","))
475-
.map(MimeMessageParser::decodeText)
475+
// and while we're at it, properly decode the personal names
476+
val recipientHeader = mimeMessage.getHeader(getHeaderName(recipientType), ",");
477+
return ofNullable(recipientHeader)
478+
.map(unchecked(h -> InternetAddress.parseHeader(h, false)))
479+
.map(ias -> Arrays.stream(ias)
480+
.map(unchecked(ia -> new InternetAddress(ia.getAddress(), decodePersonalName(ia.getPersonal()))))
481+
.toArray(Address[]::new))
476482
.orElse(null);
477-
return (s == null) ? null : InternetAddress.parseHeader(s, false);
478483
} catch (final MessagingException e) {
479484
throw new MimeMessageParseException(format(MimeMessageParseException.ERROR_GETTING_RECIPIENTS, recipientType), e);
480485
}
@@ -490,6 +495,12 @@ private static String getHeaderName(RecipientType recipientType) {
490495
return "Bcc";
491496
}
492497
}
498+
499+
@Nullable
500+
private static String decodePersonalName(String personalName) {
501+
return personalName != null ? decodeText(personalName) : null;
502+
}
503+
493504
@Nullable
494505
public static String parseContentDescription(@NotNull final MimePart mimePart) {
495506
try {

modules/simple-java-mail/src/test/java/org/simplejavamail/converter/EmailConverterTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ public void testProblematicEmbeddedImage() {
148148
assertThat(s1.getHTMLText()).containsPattern("\"cid:DB294AA3-160F-4825-923A-B16C8B674543@home\"");
149149
}
150150

151+
@Test
152+
public void testProblematicCommasInRecipeints() {
153+
Email s1 = EmailConverter.emlToEmail(new File(RESOURCE_TEST_MESSAGES + "/#444 Email with encoded comma in recipients.eml"));
154+
EmailAssert.assertThat(s1).hasFromRecipient(new Recipient("Some Name, Jane Doe", "[email protected]", null));
155+
EmailAssert.assertThat(s1).hasOnlyRecipients(new Recipient("Some Name 2, John Doe", "[email protected]", TO));
156+
}
157+
151158
@Test
152159
public void testProblematicExchangeDeliveryReceipts() throws Exception {
153160
SecureTestDataHelper.runTestWithSecureTestData(passwords -> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Received: from localhost (localhost [127.0.0.1])
2+
Subject: =?iso-8859-1?Q?Test E-Mail?=
3+
From: =?iso-8859-1?Q?Some_Name=2C_Jane_Doe?= <[email protected]>
4+
To: =?iso-8859-1?Q?Some_Name_2=2C_John_Doe?= <[email protected]>
5+
Date: Thu, 17 Jul 2022 11:30:35 +0200
6+
Mime-Version: 1.0
7+
Content-Type: multipart/mixed;
8+
boundary="=_XcF8-p0YPEze5FKM4wBEsW6g4fs7szT74eXdtYFWHz2tdNBa"
9+
X-Priority: 3 (Normal)
10+
Thread-Index: AdZmVACDs7DEv8/jTk61asogqNbsIg==
11+
Message-Id: <5f22933b.3ac9.2d67554856caaa4d@local>
12+
13+
This is a multi-part message in MIME format. Your mail reader does not
14+
understand MIME message format.
15+
16+
--=_XcF8-p0YPEze5FKM4wBEsW6g4fs7szT74eXdtYFWHz2tdNBa
17+
Content-Type: multipart/alternative;
18+
boundary="=_XcF8-p5aPECh5FzV4wU-tWHDHl3ZdfMTYuEsAukbZ+BKbIZY"
19+
20+
This is a multi-part message in MIME format. Your mail reader does not
21+
understand MIME message format.
22+
23+
--=_XcF8-p5aPECh5FzV4wU-tWHDHl3ZdfMTYuEsAukbZ+BKbIZY
24+
Content-Type: text/plain; charset=iso-8859-1
25+
Content-Transfer-Encoding: quoted-printable
26+
27+
This is a multi-part message in MIME format. Your mail reader does not
28+
understand MIME message format.
29+
30+
--=_XcF8-p0YPEze5FKM4wBEsW6g4fs7szT74eXdtYFWHz2tdNBa--
31+

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@
6666
<version>1.18.22</version>
6767
<scope>provided</scope>
6868
</dependency>
69+
<dependency>
70+
<groupId>com.pivovarit</groupId>
71+
<artifactId>throwing-function</artifactId>
72+
<version>1.5.1</version>
73+
<scope>compile</scope>
74+
</dependency>
6975

7076
<!-- testing -->
7177
<dependency>

0 commit comments

Comments
 (0)