1

I have implemented an email client in java using imaps. I've written standard recursive getText() and getAttachments() methods.

My code works as intended. The only problem I encountered was when receiving forwarded emails (as .eml). I retrieve the attached email looking recursively for a body part of type message/rfc822. The content of this message is a IMAPNestedMessage. I treat the NestedMessage just like a message, trying to get its headers, text and attachments. The headers are retrieved ok, but when calling getContent() for a text or InputStream body part, the method returns a wrong content. More specifically, it returns the correct number of bytes, but starting above where it should, containing some of parts of headers.

I have tested it by printing the entire message, and each text/attachment body part content. The content of these parts is always starting 50-100 characters above the intended content. Example of text content received:

ding: 7bit Hello World ------=_Part_938426_687455718.1462780017359

And while saving the files I get one of these exceptions:

DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 3 before EOF, the 10 most recent characters were: "nt-Transfe"

DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 3 before EOF, the 10 most recent characters were: "CA0OCAwIFI"

I have tested this on multiple imap servers and it seems the only problem is on gmail. From other servers the content is retrieved successfully.

DEBUG LOG:

A654 FETCH 62 (ENVELOPE INTERNALDATE RFC822.SIZE)
* 62 FETCH (RFC822.SIZE 7044 INTERNALDATE "09-May-2016 07:50:24 +0000" ENVELOPE ("Mon, 09 May 2016 10:50:23 +0300" "Fwd: Hello" (("alex******" NIL "alex******" "memshare.eu")) (("alex******" NIL "alex******" "memshare.eu")) (("alex******" NIL "alex******" "memshare.eu")) (("Alex******" NIL "alex******" "gmail.com")) NIL NIL NIL "<a053078792******************@memshare.eu>"))
A654 OK Success
A655 FETCH 62 (BODYSTRUCTURE)
* 62 FETCH (BODYSTRUCTURE ((("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "7BIT" 6 2 NIL NIL NIL)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "QUOTED-PRINTABLE" 144 6 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "=_e8155adb3cd8aaa3ae8217deb56d1aaf") NIL NIL)("MESSAGE" "RFC822" ("NAME" "Hello.eml") NIL NIL "8BIT" 4236 ("Mon, 9 May 2016 07:46:57 +0000 (UTC)" "Hello" (("Alex******" NIL "alex******" "rocketmail.com")) (("Alex******" NIL "alex******" "rocketmail.com")) (("Alex******" NIL "alex******" "rocketmail.com")) (("Alex" NIL "alex" "memshare.eu")) NIL NIL NIL "<999048729.938428.1462780017361.JavaMail.yahoo@mail.yahoo.com>") ((("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "7BIT" 15 0 NIL NIL NIL)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "7BIT" 267 9 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "----=_Part_938426_687455718.1462780017359") NIL NIL)("TEXT" "PLAIN" NIL "<1c9e642c-9838-7ff4-9ee4-efe55bf3b744@yahoo.com>" NIL "BASE64" 18 1 NIL ("ATTACHMENT" ("FILENAME" "a.txt")) NIL) "MIXED" ("BOUNDARY" "----=_Part_938427_1069443145.1462780017361") NIL NIL) 70 NIL ("ATTACHMENT" ("FILENAME" "Hello.eml" "SIZE" "4236")) NIL) "MIXED" ("BOUNDARY" "=_299e90aa70b2ef0a4506616771ea3aa2") NIL NIL))
A655 OK Success
A656 FETCH 62 (BODY.PEEK[2.HEADER.FIELDS (Message-Id)])
* 62 FETCH (BODY[2.HEADER.FIELDS (Message-Id)] {78}
Message-ID: <999048729.******.************.JavaMail.yahoo@mail.yahoo.com>

)
A656 OK Success
A657 FETCH 62 (BODY[2])
* 62 FETCH (BODY[2] {4236}
Return-Path: <alex******@rocketmail.com>
Delivered-To: alex******@memshare.eu
Received: from localhost (localhost [127.0.0.1])
    by mail.sunete.net (Postfix) with ESMTP id D353A4632CA
    for <alex******@memshare.eu>; Mon,  9 May 2016 10:49:45 +0300 (EEST)
X-Virus-Scanned: Debian amavisd-new at mail.sunete.net
Received: from mail.sunete.net ([127.0.0.1])
    by localhost (mail.sunete.net [127.0.0.1]) (amavisd-new, port 10024)
    with ESMTP id a3WDvP0uGPai for <alex******@memshare.eu>;
    Mon,  9 May 2016 10:49:45 +0300 (EEST)
Received: from nm47-vm9.bullet.mail.gq1.yahoo.com (nm47-vm9.bullet.mail.gq1.yahoo.com [67.195.87.187])
    (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits))
    (No client certificate requested)
    by mail.sunete.net (Postfix) with ESMTPS id EE8CA4621EB
    for <alex******@memshare.eu>; Mon,  9 May 2016 10:49:44 +0300 (EEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rocketmail.com; s=s2048; t=1462780182; bh=y7MdUdVPBKZOOjtmd41TQxcp3nn7lKiq+NuOpRsK/QA=; h=Date:From:Reply-To:To:Subject:References:From:Subject; b=gYIL8JbBzK/EjpMGNAbERTb8L9ganJW7m4GXLzPGAjiC8r1qZ7WtzSLNPBKFnFuquUfFzQCZpcb3coTNmvUuw7miv2UfvVX+u0AfgkXB+43ERLjuEIo2tin/OPYo818c+9CPUX0Jbm7BQAWvXuPUPv87/72Mn2Lu/Dc3k2NxDTTPbeYlRnL8T1vQuNMp0PG3o3Fl6uWmwq5pYIFlAO3zV8mAJdCqhUFrZj6N4h6juizDb/Z0P4xs5ufrOV8FF0Ce9xMbBNKwIRm8e4ZbvIo1lu32mYe5bmXxrqD+swLeQBsyaGRipfUteM1Fd5m/dvmXwtHWsqryum4yQAJAdhNGBg==
Received: from [127.0.0.1] by nm47.bullet.mail.gq1.yahoo.com with NNFMP; 09 May 2016 07:49:42 -0000
Received: from [98.137.12.175] by nm47.bullet.mail.gq1.yahoo.com with NNFMP; 09 May 2016 07:46:58 -0000
Received: from [98.139.170.182] by tm14.bullet.mail.gq1.yahoo.com with NNFMP; 09 May 2016 07:46:58 -0000
Received: from [98.139.212.234] by tm25.bullet.mail.bf1.yahoo.com with NNFMP; 09 May 2016 07:46:58 -0000
Received: from [127.0.0.1] by omp1043.mail.bf1.yahoo.com with NNFMP; 09 May 2016 07:46:58 -0000
X-Yahoo-Newman-Property: ymail-4
X-Yahoo-Newman-Id: 99369.43294.bm@omp1043.mail.bf1.yahoo.com
X-YMail-OSG: Ppr_828VM1nyHCvZ3utroFkl2Yq5Jg.KV6cGfgR9wadaZIYls5ras2yUCl1GfOa
 cNAcncgGsJXk967eMcJtwkMgAGbFDO3sUYw5X6W0xsjZ8VqhzrSr5wSm1DKNheVfOOqsOj9PCBrV
 A4KDEp.a4jvHr0qo_8umu0I__5D5ewz8NeHUGU.pLpFOa46.03_Kr6QZuiWvOTlVFQKz6cT2h9ul
 GT7uG9vovlavSsaN2kAEVpZqg9qU19QObMRdRvmQgXEMLn8BcNpGVSjJ13NsveT36wQfnpJnd4fa
 BIm__YZxpjtUc4q.oSc_Af5FJEr03HJnFsC9tDpV2Fy.94EfBozt_.2mDJOjsg0f9t1TWunMnQ9S
 _TaPLkeNPBbGzrpkwajgM8ltgvA59fUJv1iRc9Gc4sUVBVmL2SCc9K14kUux.algt21FhDrOXscI
 ndL1ByTKd.jT2PsD6bZ3CXcTGFEigyOBZ41kGgmmqur1qgnE2cfzb1a8xNr9RS7cZt9YPt.Xetj.
 sQHBdAP7GES1XbmVkvlYi_QB63zEln9guF8_OVuSvIDgqWs2PDUGir2iNDqz2LuM-
Received: from jws106148.mail.bf1.yahoo.com by sendmailws140.mail.bf1.yahoo.com; Mon, 09 May 2016 07:46:57 +0000; 1462780017.599
Date: Mon, 9 May 2016 07:46:57 +0000 (UTC)
From: Alexandru Severin <alex******@rocketmail.com>
Reply-To: Alexandru Severin <alex******@rocketmail.com>
To: Alex <alex******@memshare.eu>
Message-ID: <999048729.938428.1462780017361.JavaMail.yahoo@mail.yahoo.com>
Subject: Hello
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_938427_1069443145.1462780017361"
References: <999048729.938428.1462780017361.JavaMail.yahoo.ref@mail.yahoo.com>

------=_Part_938427_1069443145.1462780017361
Content-Type: multipart/alternative; 
    boundary="----=_Part_938426_687455718.1462780017359"

------=_Part_938426_687455718.1462780017359
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Hello World


------=_Part_938426_687455718.1462780017359
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px">Hello World<br><div id="yui_3_16_0_ym19_1_1462779166408_5257"><br></div></div></body></html>
------=_Part_938426_687455718.1462780017359--

------=_Part_938427_1069443145.1462780017361
Content-Type: text/plain
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=a.txt
Content-ID: <1c9e642c-9838-7ff4-9ee4-efe55bf3b744@yahoo.com>

aGVsbG8gd29ybGQK

------=_Part_938427_1069443145.1462780017361--
)
A657 OK Success
A658 FETCH 62 (BODY[2.1.1])
* 62 FETCH (BODY[2.1.1] {15}
6_687455718.146)
A658 OK Success
A659 FETCH 62 (BODY[2.1.2])
* 62 FETCH (BODY[2.1.2] {267}
ding: 7bit

Hello World

------=_Part_938426_687455718.1462780017359
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, H)
A659 OK Success
A660 FETCH 62 (BODY[2.2])
* 62 FETCH (BODY[2.2] {18}
n
Content-Transfe)
A660 OK Success

com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 3 before EOF, the 10 most recent characters were: "nt-Transfe"
    at com.sun.mail.util.BASE64DecoderStream.decode(BASE64DecoderStream.java:260)
    at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:144)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at com.sun.mail.handlers.text_plain.getContent(text_plain.java:98)

The gmail client displays these emails correctly. Is there any known workaround this issue?

Alexandru Severin
  • 6,021
  • 11
  • 48
  • 71
  • What mail server are you using? Some IMAP servers have problems with attachments that are messages. Can you post the [JavaMail debug output](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug) when processing such a message? – Bill Shannon May 01 '16 at 21:58
  • @BillShannon I am using `imap.gmail.com`. I added the output for bodyparts 2.1.2 (the text) and 2.2 (the file) – Alexandru Severin May 04 '16 at 07:26
  • I don't see the debug output. Did you follow my link above? Based on your edits above, it sounds like you've discovered a bug in Gmail. To prove it, I need to see the debug output. – Bill Shannon May 07 '16 at 06:40
  • @BillShannon I added it again, this time the entire log for one email. The `text/html` is being retrieved from 2.1.2 body part, and the attachment from `2.2` – Alexandru Severin May 09 '16 at 08:12
  • 1
    Based on your debug logs, that definitely looks like a bug in gmail. That 'ding: 7bit' is in the output it returned. – Max May 09 '16 at 18:51
  • Yup, looks like a Gmail bug. Can you reproduce this with any message that contains another message as an attachment? Or only with this particular message in this particular folder? – Bill Shannon May 12 '16 at 06:46
  • @BillShannon - all .eml attachments from gmail. I realised its a bug, I was wondering if there is any workaround available for it. I'm trying to avoid manually parsing the file – Alexandru Severin May 12 '16 at 09:41
  • The standard workaround is described in the [JavaMail FAQ](http://www.oracle.com/technetwork/java/javamail/faq/index.html#imapserverbug). – Bill Shannon May 14 '16 at 07:09
  • I've reproduced this Gmail bug and reported it to my contact at Google. – Bill Shannon May 17 '16 at 19:34

0 Answers0