From b2dd234e7d74c90e021050c77818a96437071cbc Mon Sep 17 00:00:00 2001 From: joshihirenn Date: Wed, 15 Sep 2021 11:04:09 +0530 Subject: [PATCH 1/2] End MIME boundary should be followed by CRLF The present code mandates that a CRLF is present after intermediate MIME boundary (that starts with --). This change extends that behaviour for end boundary too (that starts and ends with --). This is useful for processing some bulk emails that uses a single dash character as a boundary and a line full of dashes is present in the content. Gmail and Outlook are able to process such emails, however JavaMail truncates the email after the sequence of five dash characters are consumed from the line full of dash characters. --- .../jakarta/mail/internet/MimeMultipart.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/jakarta/mail/internet/MimeMultipart.java b/api/src/main/java/jakarta/mail/internet/MimeMultipart.java index 03f47293..78f52f16 100644 --- a/api/src/main/java/jakarta/mail/internet/MimeMultipart.java +++ b/api/src/main/java/jakarta/mail/internet/MimeMultipart.java @@ -806,9 +806,25 @@ protected synchronized void parse() throws MessagingException { int b2 = in.read(); if (b2 == '-') { if (in.read() == '-') { - complete = true; - done = true; - break; // ignore trailing text + int b3 = in.read(); + // skip linear whitespace + while (b3 == ' ' || b3 == '\t') + { b3 = in.read(); } + if (b3 == '\n' || b3 == '\r') + { + if(b3 == '\r') + { + // consume '\n' if it follows, so that next read + // starts from first character of the epilogue. + // useful if in future epilogue extraction is added + in.mark(1); + if (in.read() != '\n') + { in.reset(); } + } + complete = true; + done = true; + break; // ignore trailing text + } } } // skip linear whitespace From 51bb06aafc0e070617d85db0c2ac5c008dbc192e Mon Sep 17 00:00:00 2001 From: joshihirenn Date: Wed, 15 Sep 2021 19:17:38 +0530 Subject: [PATCH 2/2] Handle case where there is EOF after end MIME boundary No all messages have epilogue. So an end MIME boundary may be followed by EOF. --- api/src/main/java/jakarta/mail/internet/MimeMultipart.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/jakarta/mail/internet/MimeMultipart.java b/api/src/main/java/jakarta/mail/internet/MimeMultipart.java index 78f52f16..c54be22b 100644 --- a/api/src/main/java/jakarta/mail/internet/MimeMultipart.java +++ b/api/src/main/java/jakarta/mail/internet/MimeMultipart.java @@ -810,7 +810,7 @@ protected synchronized void parse() throws MessagingException { // skip linear whitespace while (b3 == ' ' || b3 == '\t') { b3 = in.read(); } - if (b3 == '\n' || b3 == '\r') + if (b3 == '\n' || b3 == '\r' || b3 == -1) { if(b3 == '\r') {