Skip to content

Commit

Permalink
#230: properly handle missing address value in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
bbottema committed Oct 20, 2019
1 parent 3603f56 commit 5ba691e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class MimeMessageParseException extends MailException {

static final String ERROR_PARSING_FROMADDRESS = "Error parsing from-address";
static final String ERROR_PARSING_ADDRESS = "Error parsing [%s] address";
static final String ERROR_PARSING_ADDRESS = "Error parsing [%s] address [%s]";
static final String ERROR_PARSING_DISPOSITION = "Error parsing MimeMessage disposition";
static final String ERROR_PARSING_CONTENT = "Error parsing MimeMessage Content";
static final String ERROR_PARSING_MULTIPART_COUNT = "Error parsing MimeMessage multipart count";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ private static void parseMimePartTree(@Nonnull final MimePart currentPart, @Nonn
}
}
}

@SuppressWarnings("StatementWithEmptyBody")
private static void parseHeader(final Header header, @Nonnull final ParsedMimeMessageComponents parsedComponents) {
if (header.getName().equals("Disposition-Notification-To")) {
parsedComponents.dispositionNotificationTo = createAddress(header, "Disposition-Notification-To");
parsedComponents.dispositionNotificationTo = createAddress(header.getValue(), "Disposition-Notification-To");
} else if (header.getName().equals("Return-Receipt-To")) {
parsedComponents.returnReceiptTo = createAddress(header, "Return-Receipt-To");
parsedComponents.returnReceiptTo = createAddress(header.getValue(), "Return-Receipt-To");
} else if (header.getName().equals("Return-Path")) {
parsedComponents.bounceToAddress = createAddress(header, "Return-Path");
parsedComponents.bounceToAddress = createAddress(header.getValue(), "Return-Path");
} else if (!HEADERS_TO_IGNORE.contains(header.getName())) {
parsedComponents.headers.put(header.getName(), header.getValue());
} else {
Expand Down Expand Up @@ -245,12 +245,15 @@ public static List<Header> retrieveAllHeaders(@Nonnull final MimePart part) {
}
}

@Nonnull
private static InternetAddress createAddress(final Header header, final String typeOfAddress) {
@Nullable
static InternetAddress createAddress(final String address, final String typeOfAddress) {
try {
return new InternetAddress(header.getValue());
return new InternetAddress(address);
} catch (final AddressException e) {
throw new MimeMessageParseException(format(MimeMessageParseException.ERROR_PARSING_ADDRESS, typeOfAddress), e);
if (e.getMessage().equals("Empty address")) {
return null;
}
throw new MimeMessageParseException(format(MimeMessageParseException.ERROR_PARSING_ADDRESS, typeOfAddress, address), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.simplejavamail.converter.internal.mimemessage;

import org.assertj.core.api.ThrowableAssert;
import org.junit.Test;
import org.simplejavamail.converter.EmailConverter;
import org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.ParsedMimeMessageComponents;
Expand All @@ -9,11 +10,15 @@
import org.simplejavamail.email.Recipient;
import testutil.ConfigLoaderTestHelper;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

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

public class MimeMessageParserTest {
Expand Down Expand Up @@ -51,7 +56,37 @@ public void testMoveInvalidEmbeddedResourcesToAttachments_Invalid() throws IOExc
assertThat(parsedComponents.cidMap).containsOnlyKeys("moo1");
assertThat(parsedComponents.attachmentList).extracting("key").containsOnly("moo2");
}


@Test
public void testCreateAddress() throws AddressException, UnsupportedEncodingException {
assertThat(interpretRecipient("[email protected]")).isEqualTo(new InternetAddress("[email protected]", null));
assertThat(interpretRecipient(" [email protected] ")).isEqualTo(new InternetAddress("[email protected]", null));
assertThat(interpretRecipient(" <[email protected]> ")).isEqualTo(new InternetAddress("[email protected]", null));
assertThat(interpretRecipient(" < [email protected] > ")).isEqualTo(new InternetAddress("[email protected]", null));
assertThat(interpretRecipient("moo <[email protected]>")).isEqualTo(new InternetAddress("[email protected]", "moo"));
assertThat(interpretRecipient("moo<[email protected]>")).isEqualTo(new InternetAddress("[email protected]", "moo"));
assertThat(interpretRecipient(" moo< [email protected] > ")).isEqualTo(new InternetAddress("[email protected]", "moo"));
assertThat(interpretRecipient("\"moo\" <[email protected]>")).isEqualTo(new InternetAddress("[email protected]", "moo"));
assertThat(interpretRecipient("\"moo\"<[email protected]>")).isEqualTo(new InternetAddress("[email protected]", "moo"));
assertThat(interpretRecipient(" \"moo\"< [email protected] > ")).isEqualTo(new InternetAddress("[email protected]", "moo"));
assertThat(interpretRecipient(" \" m oo \"< [email protected] > ")).isEqualTo(new InternetAddress("[email protected]", " m oo "));
assertThat(interpretRecipient("< >")).isNull();

// next one is unparsable by InternetAddress#parse(), so it should be taken as is
assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
@Override
public void call() throws Throwable {
interpretRecipient(" \" m oo \" [email protected] ");
}
})
.isInstanceOf(MimeMessageParseException.class)
.hasMessage("Error parsing [TO] address [ \" m oo \" [email protected] ]");
}

private InternetAddress interpretRecipient(String address) {
return MimeMessageParser.createAddress(address, "TO");
}

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

0 comments on commit 5ba691e

Please sign in to comment.