Skip to content

Commit

Permalink
Ignore invalid headers when converting to msg->mbox/eml. Fix #231
Browse files Browse the repository at this point in the history
  • Loading branch information
lolo101 committed Nov 19, 2023
1 parent 63bd595 commit 5148156
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ private static void addHeaders(Message msg, Part jmsg) throws MessagingException
while (!lines.isEmpty()) {
String headerLine = lines.remove();
int separatorIndex = headerLine.indexOf(':');
String name = headerLine.substring(0, separatorIndex);
String value = accumulateValue(lines, headerLine.substring(separatorIndex + 1));
jmsg.addHeader(name, value);
if (separatorIndex > 0) {
String name = headerLine.substring(0, separatorIndex);
String value = accumulateValue(lines, headerLine.substring(separatorIndex + 1));
jmsg.addHeader(name, value);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

class MBoxWriterViaJavaMailTest {
@Test
Expand Down Expand Up @@ -92,6 +93,21 @@ void testIssue178() throws Exception {
}
}

@Test
void testIssue231() throws Exception {
ModuleLauncher.BaseConfigureLogging();

try (FileSystem fileSystem = Jimfs.newFileSystem()) {
Path testOut = fileSystem.getPath("test_out.eml");
try (OutputStream outputStream = Files.newOutputStream(testOut)) {
Message msg = new Message();
msg.setHeaders("key without value\n");
MBoxWriterViaJavaMail writer = givenWriter();
assertThatCode(() -> writer.write(msg, outputStream)).doesNotThrowAnyException();
}
}
}

private static Message givenMessage(String name) throws Exception {
URI uri = MBoxWriterViaJavaMailTest.class.getResource(name).toURI();
return new MessageParser(Path.of(uri)).parseMessage();
Expand Down

0 comments on commit 5148156

Please sign in to comment.