0
votes

Mail server: James. Mail container: Maria DB

Mail parsing source is like below:

    Return-Path: <[email protected]>
    Delivered-To: [email protected]
    Received: from 192.168.10.159 ([192.168.10.159])
              by WIN-55ERUE9ID5R (JAMES SMTP Server 2.3.2) with SMTP ID 374
              for <[email protected]>;
              Sat, 14 Nov 2015 16:08:06 +0900 (KST)
    Received: from unknown (HELO cas01.bsecm.com) (192.168.10.14)
        by 192.168.10.159 with ESMTP; 14 Nov 2015 16:07:48 +0900
    X-Original-SENDERIP: 192.168.10.14
    X-Original-MAILFROM: [email protected]
    X-Original-RCPTTO: [email protected]
    Resent-From: <[email protected]>
    Received: from spam.bsecm.com (192.168.10.159) by cas01.bsecm.com
     (192.168.10.14) with Microsoft SMTP Server (TLS) id 14.3.224.2; Sat, 14 Nov
     2015 16:07:42 +0900
    Received: from unknown (HELO ?219.255.136.51?) (219.255.136.51) by
     192.168.10.159 with ESMTP; 14 Nov 2015 16:07:47 +0900
    X-Original-SENDERIP: 219.255.136.51
    X-Original-MAILFROM: [email protected]
    X-Original-RCPTTO: [email protected]
    Date: Sat, 14 Nov 2015 16:07:56 +0900
    Subject: =?euc-kr?B?W0tCxKu15Vdpc2VJbmZvIMfDt6+9ul3AscDnyKO01MDHIDExv/kgvcW/68Gkurizu7+qvK3A1LTPtNku?=
    From: =?euc-kr?B?v8PFqbe5tfc=?= <[email protected]>
    To: <[email protected]>
    Reply-To: <[email protected]>
    MIME-Version: 1.0
    Content-Transfer-Encoding: BASE64
    X-Mailer: Netpion Service Server v3.0.0
    X-NetpionMsgID: 1447484876647.10284.1455.759978089,KB06,1006190838.1
    Content-Type: multipart/mixed;
        boundary="--Netpathy_Netpion.1447484876647.AA"
    Message-ID: <[email protected]>

    LS0tLU5ldHBhdGh5X05ldHBpb24uMTQ0NzQ4NDg3NjY0Ny5BQQ0KQ29udGVudC1UeXBlOiBtdWx0
    aXBhcnQvYWx0ZXJuYXRpdmU7DQoJYm91bmRhcnk9Ii0tTmV0cGF0aHlfTmV0cGlvbi4xNDQ3NDg0
    ODc2NjQ3LlpaIg0KDQotLS0tTmV0cGF0aHlfTmV0cGlvbi4xNDQ3NDg0ODc2NjQ3LlpaDQpDb250

NEthVzVwZENncE93bzgNCkwzTmpjbWx3ZEQ0PQ0KDQotLS0tTmV0cGF0aHlfTmV0cGlvbi4xNDQ3
    NDg0ODc2NjQ3LkFBLS0NCg==
  1. The contents is encoded by BASE64. At "Content-Transfer-Encoding: BASE64".
  2. When I get contents Java tells me "Missing start boundary" exception.
byte[] messageBody = (byte[])("*FROM DB BLOB DATA*");
Message jamesMail = new MimeMessage(session, new ByteArrayInputStream(messageBody) );
Multipart mp = (Multipart)jamesMail.getContent();
int i = mp.getCount();
  1. And now I decode mail contents. This can parse mail contents to be readable.
java.io.InputStream is = jamesMail.getInputStream();
java.io.InputStream decodedIs = MimeUtility.decode(is, contentTransferEncoding);
mp = (Multipart)ms.getContent();
for(int m=0; m < mp.getCount(); m++){
// contents text processing
// attached file processing
}
  1. But, mail contents has an attached file with encoded by BASE64. And my code can not parse that attached file.

  2. Question -. How can I parsing pre-encoded mail contents using Javamail? -. How can I parsing pre-encoded attachment file?

2

2 Answers

0
votes

The message is incorrectly formatted. Multipart content MUST not be encoded, only "leaf" body parts may be encoded. "Netpion Service Server" is broken, please report the bug to the owner of that software.

You can work around this bug in the server by setting the "mail.mime.ignoremultipartencoding" System property to "false".

0
votes

Temporary Solution)
I made a message [Header] + [ BASE64 Decoded Contents]

String headerLines = "";
String contentTransferEncoding = "";
while (headers.hasMoreElements()) {
  Header h = (Header) headers.nextElement();
  headerLines += String.format("%s: %s\r\n",h.getName(),h.getValue());
  if(h.getName().equalsIgnoreCase("Content-Transfer-Encoding")){
    contentTransferEncoding = (StringUtils.isEmpty(h.getValue()))?"":h.getValue();
  }
}
InputStream hStream = new ByteArrayInputStream(headerLines.getBytes());
InputStream is = orgJamesMail.getInputStream();
InputStream decodedIs = MimeUtility.decode(is, contentTransferEncoding);
Message ms = new MimeMessage(session, mm);
mp = (Multipart)ms.getContent();

1. Get header information by String.
2. Convert header information of String to InputStream.
3. Concatenate "header inputstream" and "decoded contents inputstream"
4. And parse concatenated message stream.
Maybe this is need:
System.setProperty("mail.mime.multipart.ignoreexistingboundaryparameter", "false");