2015-08-14 118 views
3

我有以下MIME消息:MIME郵件無法解析

Return-Path: <[email protected]> 
X-Original-To: [email protected] 
Delivered-To: [email protected] 
Received: from localhost [127.0.0.1] 
    by unify-prod with POP3 (fetchmail-6.3.21) 
    for <[email protected]> (single-drop); Mon, 03 Aug 2015 12:42:24+0000 (UTC) 
Received: from testdomain.com (testdomain.com [192.69.176.183]) 
    by unify.test.com (Postfix) with ESMTPS id AA3874330B 
    for <[email protected]>; Mon, 3 Aug 2015 12:42:23 +0000 (UTC) 
Received: from test.call (test.call [10.3.1.49]) 
    by test.com (Postfix) with ESMTP id 56EC73BA8C4 
    for <[email protected]>; Mon, 3 Aug 2015 12:42:23 +0000 (UTC) 
Date: Mon, 3 Aug 2015 12:42:23 +0000 (UTC) 
From: [email protected] 
To: [email protected] 
Message-ID: <[email protected]> 
Subject: Update 
MIME-Version: 1.0 
Content-Type: text/plain; charset=UTF-8 
Content-Transfer-Encoding: 7bit 



|0004625641| 
|630805367| 
|NA14220388| 
|03.08.2015 14:42:23| 


|| 

我想這個代碼來解析Grails的消息:

InputStream mailFileInputStream = new FileInputStream("/home/peter/test.msg"); 
    Properties props = new Properties(); 
    Session session = Session.getDefaultInstance(props, null); 
    MimeMessage message = new MimeMessage(session, mailFileInputStream); 

    MimeMessageParser parser = new MimeMessageParser(message) 
    parser.parse() 


    def data = [:] 
    data.from = parser.getFrom() 
    data.to = parser.getTo() 
    data.replyTo = parser.getReplyTo() 
    data.html = parser.getHtmlContent() 
    data.plain = parser.getPlainContent() 
    data.subject = parser.getSubject() 
    data.attachments = parser.getAttachmentList() 

但沒有正確解析的消息。整個信息都在簡單的內容中。這條消息有什麼問題?

最好的問候, 彼得

回答

0

當我試圖分析我得到這個異常的消息:

java.lang.ClassCastException: javax.mail.util.SharedByteArrayInputStream cannot be cast to java.lang.String 
    at org.apache.commons.mail.util.MimeMessageParser.parse(MimeMessageParser.java:181) 
    at org.apache.commons.mail.util.MimeMessageParser.parse(MimeMessageParser.java:96) 
    at org.apache.commons.mail.util.MimeMessageParser$parse.call(Unknown Source) 

的問題是,MimeMessageParser試圖強迫一個對象字符串。您可以在線路181上的source code中看到此對象。該對象是javax.mail.util.SharedByteArrayInputStream的一個實例,它不會通過強制來返回內容。我的例子使用一個StringBufferInputStream作爲輸入,因此可能會在問題中發揮作用。

變通

要解決這個問題,我推翻MimeMessage.getContent(),所以它返回純文本內容。很明顯,不要用非純文本內容來做到這一點。以下是完整的腳本:

@Grab('org.apache.commons:commons-email:1.4') 

import javax.mail.Session 
import javax.mail.internet.MimeMessage 
import org.apache.commons.mail.util.MimeMessageParser 

def input = '''Return-Path: <[email protected]> 
X-Original-To: [email protected] 
Delivered-To: [email protected] 
Received: from localhost [127.0.0.1] 
    by unify-prod with POP3 (fetchmail-6.3.21) 
    for <[email protected]> (single-drop); Mon, 03 Aug 2015 12:42:24+0000 (UTC) 
Received: from testdomain.com (testdomain.com [192.69.176.183]) 
    by unify.test.com (Postfix) with ESMTPS id AA3874330B 
    for <[email protected]>; Mon, 3 Aug 2015 12:42:23 +0000 (UTC) 
Received: from test.call (test.call [10.3.1.49]) 
    by test.com (Postfix) with ESMTP id 56EC73BA8C4 
    for <[email protected]>; Mon, 3 Aug 2015 12:42:23 +0000 (UTC) 
Date: Mon, 3 Aug 2015 12:42:23 +0000 (UTC) 
From: [email protected] 
To: [email protected] 
Message-ID: <[email protected]> 
Subject: Update 
MIME-Version: 1.0 
Content-Type: text/plain; charset=UTF-8 
Content-Transfer-Encoding: 7bit 



|0004625641| 
|630805367| 
|NA14220388| 
|03.08.2015 14:42:23| 


||''' 

// Creates a MimeMessage that makes MimeMessageParser happy. 
MimeMessage createMimeMessage(InputStream stream) { 
    def getContent = MimeMessage.metaClass.getMetaMethod('getContent') 
    def message = new MimeMessage(
     Session.getDefaultInstance(new Properties()), 
     stream) 

    /* 
    * Override MimeMessage.getContent() 
    * to coerce SharedByteArrayInputStream 
    * into a String. Groovy does it automatically :) 
    */ 
    message.metaClass.getContent = { 
     getContent.invoke(delegate) 
    } 

    return message 
} 

def message = createMimeMessage(new StringBufferInputStream(input)) 
def parser = new MimeMessageParser(message) 

parser.parse() 

def data = parser.with { 
    [ 
     from: from, 
     to: to, 
     replyTo: replyTo, 
     html: htmlContent, 
     plain: plainContent, 
     subject: subject, 
     attachments: attachmentList, 
    ] 
} 

assert data.from == '[email protected]' 
assert data.to instanceof List 
assert data.replyTo == '[email protected]' 
assert data.html == null 
assert data.attachments == [] 
assert data.plain instanceof String 

數據地圖包含以下內容:

[from:[email protected], to:[[email protected]], replyTo:[email protected], html:null, plain: 

|0004625641| 
|630805367| 
|NA14220388| 
|03.08.2015 14:42:23| 


||, subject:Update, attachments:[]] 
0

從Java郵件API如下嘗試:

import javax.mail.internet.*; 
import javax.mail.*; 

def message = '''Return-Path: <[email protected]> 
X-Original-To: [email protected] 
Delivered-To: [email protected] 
Received: from localhost [127.0.0.1] 
    by unify-prod with POP3 (fetchmail-6.3.21) 
    for <[email protected]> (single-drop); Mon, 03 Aug 2015 12:42:24+0000 (UTC) 
Received: from testdomain.com (testdomain.com [192.69.176.183]) 

    by unify.test.com (Postfix) with ESMTPS id AA3874330B 
    for <[email protected]>; Mon, 3 Aug 2015 12:42:23 +0000 (UTC) 
Received: from test.call (test.call [10.3.1.49]) 
    by test.com (Postfix) with ESMTP id 56EC73BA8C4 
    for <[email protected]>; Mon, 3 Aug 2015 12:42:23 +0000 (UTC) 
Date: Mon, 3 Aug 2015 12:42:23 +0000 (UTC) 
From: [email protected] 
To: [email protected] 
Message-ID: <[email protected]> 
Subject: Update 
MIME-Version: 1.0 
Content-Type: text/plain; charset=UTF-8 
Content-Transfer-Encoding: 7bit 



|0004625641| 
|630805367| 
|NA14220388| 
|03.08.2015 14:42:23| 


||''' 

InputStream mailFileInputStream = new FileInputStream(message);  
Properties props = new Properties();  
Session session = Session.getDefaultInstance(props, null);  
MimeMessage message1 = new MimeMessage(session, mailFileInputStream);  

message1.getFrom()println "-----data--${message1.getRecipients()}----${message1.getAllHeaders()}"​​​​​​​​​​​ 

這必須給予

java.security.AccessControlException: access denied ("java.io.FilePermission" 

可以通過以下方式解決:java.security.AccessControlException: Access denied (java.io.FilePermission