2015-08-14 4 views
3

Я следующий мимом сообщением: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| 


|| 

Я хочу, чтобы разобрать сообщение в Грааль с этим кодом:

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 пытается принудить к объект в Строка. Вы можете увидеть это в source code в строке 181. Объект представляет собой экземпляр 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 Mail 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

Смежные вопросы