当邮件内容由BASE64编码时,由JavaMail进行电子邮件解析

时间:2015-11-18 06:16:48

标签: javamail

邮件服务器:詹姆斯。 邮件容器:Maria DB

邮件解析源如下所示:

    Return-Path: <monad@monad.com>
    Delivered-To: yoonsang@bsecm.net
    Received: from 192.168.10.159 ([192.168.10.159])
              by WIN-55ERUE9ID5R (JAMES SMTP Server 2.3.2) with SMTP ID 374
              for <monad@monad.net>;
              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: monad@monad.com
    X-Original-RCPTTO: monad@monad.net
    Resent-From: <monad@monad.com>
    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: allcredit@allcredit.co.kr
    X-Original-RCPTTO: monad@monad.com
    Date: Sat, 14 Nov 2015 16:07:56 +0900
    Subject: =?euc-kr?B?W0tCxKu15Vdpc2VJbmZvIMfDt6+9ul3AscDnyKO01MDHIDExv/kgvcW/68Gkurizu7+qvK3A1LTPtNku?=
    From: =?euc-kr?B?v8PFqbe5tfc=?= <allcredit@allcredit.co.kr>
    To: <monad@monad.com>
    Reply-To: <allcredit@allcredit.co.kr>
    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: <11342c7a-220f-4538-9203-3610a6896f3e@CAS01.bsecm.com>

    LS0tLU5ldHBhdGh5X05ldHBpb24uMTQ0NzQ4NDg3NjY0Ny5BQQ0KQ29udGVudC1UeXBlOiBtdWx0
    aXBhcnQvYWx0ZXJuYXRpdmU7DQoJYm91bmRhcnk9Ii0tTmV0cGF0aHlfTmV0cGlvbi4xNDQ3NDg0
    ODc2NjQ3LlpaIg0KDQotLS0tTmV0cGF0aHlfTmV0cGlvbi4xNDQ3NDg0ODc2NjQ3LlpaDQpDb250

NEthVzVwZENncE93bzgNCkwzTmpjbWx3ZEQ0PQ0KDQotLS0tTmV0cGF0aHlfTmV0cGlvbi4xNDQ3
    NDg0ODc2NjQ3LkFBLS0NCg==
  1. 内容由BASE64编码。在“Content-Transfer-Encoding:BASE64”。
  2. 当我得到内容时,Java告诉我“缺少开始边界”异常。
  3. 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. 现在我解码邮件内容了。 这可以解析邮件内容的可读性。
    2. 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. 但是,邮件内容有一个由BASE64编码的附件。 我的代码无法解析附件。

      2. 问题 - 。如何使用Javamail解析预编码的邮件内容? - 。如何解析预编码的附件?

2 个答案:

答案 0 :(得分:0)

邮件格式不正确。不得对多部分内容进行编码,只能对“叶子”正文部分进行编码。 “Netpion Service Server”已损坏,请将错误报告给该软件的所有者。

您可以通过setting the "mail.mime.ignoremultipartencoding" System property to "false"在服务器中解决此错误。

答案 1 :(得分:0)

临时解决方案)
我发了一条消息[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。按字符串获取标题信息 2.将String的头信息转换为InputStream 3.连接&#34;标头输入流&#34;和#34;解码内容输入流&#34;
4.解析串联的消息流 也许这是需要的:
System.setProperty(&#34; mail.mime.multipart.ignoreexistingboundaryparameter&#34;,&#34; false&#34;);