Grails 无法分析Mime消息

Grails 无法分析Mime消息,grails,mime-mail,Grails,Mime Mail,我有以下mime消息: Return-Path: <some@adress.com> X-Original-To: some@adress.com Delivered-To: some@adress.com Received: from localhost [127.0.0.1] by unify-prod with POP3 (fetchmail-6.3.21) for <other@localhost> (single-drop); Mon, 03 A

我有以下mime消息:

Return-Path: <some@adress.com>
X-Original-To: some@adress.com
Delivered-To: some@adress.com
Received: from localhost [127.0.0.1]
    by unify-prod with POP3 (fetchmail-6.3.21)
    for <other@localhost> (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 <test@test.com>; 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 <test@test.com>; Mon,  3 Aug 2015 12:42:23 +0000 (UTC)
Date: Mon, 3 Aug 2015 12:42:23 +0000 (UTC)
From: test@test.com
To: test@test.com
Message-ID: <385098705.398991.1438605743348.JavaMail.sd@sdapp04>
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|


||
但该消息未正确解析。整个信息都是简单的内容。此消息有什么问题

致以最良好的祝愿,
Peter

当我试图解析该消息时,出现以下异常:

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)
问题是mimessageparser试图将对象强制转换为字符串。你可以在181线上看到这一点。该对象是javax.mail.util.SharedByteArrayInputStream的实例,它不会通过强制返回内容。我的示例使用StringBufferInputStream作为输入,因此这可能是问题的一部分

变通 为了解决这个问题,我重写了mimessage.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: <some@adress.com>
X-Original-To: some@adress.com
Delivered-To: some@adress.com
Received: from localhost [127.0.0.1]
    by unify-prod with POP3 (fetchmail-6.3.21)
    for <other@localhost> (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 <test@test.com>; 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 <test@test.com>; Mon,  3 Aug 2015 12:42:23 +0000 (UTC)
Date: Mon, 3 Aug 2015 12:42:23 +0000 (UTC)
From: test@test.com
To: test@test.com
Message-ID: <385098705.398991.1438605743348.JavaMail.sd@sdapp04>
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 == 'test@test.com'
assert data.to instanceof List
assert data.replyTo == 'test@test.com'
assert data.html == null
assert data.attachments == []
assert data.plain instanceof String

请从java mail api中尝试以下内容:

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

def message = '''Return-Path: <some@adress.com>
X-Original-To: some@adress.com
Delivered-To: some@adress.com
Received: from localhost [127.0.0.1]
    by unify-prod with POP3 (fetchmail-6.3.21)
    for <other@localhost> (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 <test@test.com>; 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 <test@test.com>; Mon,  3 Aug 2015 12:42:23 +0000 (UTC)
Date: Mon, 3 Aug 2015 12:42:23 +0000 (UTC)
From: test@test.com
To: test@test.com
Message-ID: <385098705.398991.1438605743348.JavaMail.sd@sdapp04>
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()}"​​​​​​​​​​​
这可以通过以下方法解决

[from:test@test.com, to:[test@test.com], replyTo:test@test.com, html:null, plain:

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


||, subject:Update, attachments:[]]
import javax.mail.internet.*;
import javax.mail.*;

def message = '''Return-Path: <some@adress.com>
X-Original-To: some@adress.com
Delivered-To: some@adress.com
Received: from localhost [127.0.0.1]
    by unify-prod with POP3 (fetchmail-6.3.21)
    for <other@localhost> (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 <test@test.com>; 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 <test@test.com>; Mon,  3 Aug 2015 12:42:23 +0000 (UTC)
Date: Mon, 3 Aug 2015 12:42:23 +0000 (UTC)
From: test@test.com
To: test@test.com
Message-ID: <385098705.398991.1438605743348.JavaMail.sd@sdapp04>
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"