Skip to content

Commit

Permalink
#227: make parsing recipients more lenient allows semicolons at the e…
Browse files Browse the repository at this point in the history
…nd of a list of one addresses
  • Loading branch information
Benny Bottema authored and Benny Bottema committed Sep 27, 2019
1 parent aa5f8c8 commit 21e5060
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.simplejavamail.converter.internal.mimemessage;

import org.simplejavamail.internal.util.NaturalEntryKeyComparator;
import org.simplejavamail.internal.util.Preconditions;

import javax.activation.DataHandler;
import javax.activation.DataSource;
Expand Down Expand Up @@ -390,12 +391,26 @@ public static List<InternetAddress> parseBccAddresses(@Nonnull final MimeMessage
@Nullable
public static Address[] retrieveRecipients(@Nonnull final MimeMessage mimeMessage, final RecipientType recipientType) {
try {
return mimeMessage.getRecipients(recipientType);
// return mimeMessage.getRecipients(recipientType); // can fail in strict mode, see https://github.com/bbottema/simple-java-mail/issues/227
// workaround following (copied and modified from JavaMail internal code):
String s = mimeMessage.getHeader(getHeaderName(mimeMessage, recipientType), ",");
return (s == null) ? null : InternetAddress.parseHeader(s, false);
} catch (final MessagingException e) {
throw new MimeMessageParseException(format(MimeMessageParseException.ERROR_GETTING_RECIPIENTS, recipientType), e);
}
}

private static String getHeaderName(@Nonnull MimeMessage mimeMessage, RecipientType recipientType) throws MessagingException {
if (recipientType == RecipientType.TO) {
return "To";
} else if (recipientType == RecipientType.CC) {
return "Cc";
} else {
Preconditions.assumeTrue(recipientType == RecipientType.BCC, "invalid recipient type: " + recipientType);
return "Bcc";
}
}

@Nonnull
private static List<InternetAddress> parseInternetAddresses(@Nullable final Address[] recipients) {
final List<Address> addresses = (recipients != null) ? Arrays.asList(recipients) : new ArrayList<Address>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package org.simplejavamail.converter.internal.mimemessage;

import org.junit.Test;
import org.simplejavamail.converter.EmailConverter;
import org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.ParsedMimeMessageComponents;
import org.simplejavamail.email.Email;
import org.simplejavamail.email.EmailAssert;
import org.simplejavamail.email.EmailBuilder;
import org.simplejavamail.email.Recipient;
import testutil.ConfigLoaderTestHelper;

import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;

import static javax.mail.Message.RecipientType.TO;
import static org.assertj.core.api.Assertions.assertThat;
import static org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.moveInvalidEmbeddedResourcesToAttachments;

Expand Down Expand Up @@ -44,4 +51,24 @@ public void testMoveInvalidEmbeddedResourcesToAttachments_Invalid() throws IOExc
assertThat(parsedComponents.cidMap).containsOnlyKeys("moo1");
assertThat(parsedComponents.attachmentList).extracting("key").containsOnly("moo2");
}

@Test
// https://github.com/bbottema/simple-java-mail/issues/227
public void testSemiColonSeparatedToAddresses() {
ConfigLoaderTestHelper.clearConfigProperties();

final Email initialEmail = EmailBuilder.startingBlank()
.from("lollypop", "lol.pop@somemail.com")
.to("C.Cane", "candycane@candyshop.org")
.withPlainText("We should meet up!")
.buildEmail();

String corruptedEML = EmailConverter.emailToEML(initialEmail).replace(
"To: \"C.Cane\" <candycane@candyshop.org>",
"To: \"C.Cane\" <candycane@candyshop.org>;");

final Email fixedEmail = EmailConverter.emlToEmail(corruptedEML);

EmailAssert.assertThat(fixedEmail).hasOnlyRecipients(new Recipient("C.Cane", "candycane@candyshop.org", TO));
}
}

0 comments on commit 21e5060

Please # to comment.