当前位置: 首页 > 知识库问答 >
问题:

无法解析Mime消息

诸葛砚文
2023-03-14
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("/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()

向你问好,彼得

共有1个答案

佴阳曦
2023-03-14

当我试图解析消息时,我得到了这个异常:

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行的源代码中看到这一点。该对象是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: <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
[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:[]]
 类似资料:
  • 我之前通过调用成功地用Tika解析了所有类型的文件,而没有设置任何自定义配置或元数据。现在我需要根据MIME-Type过滤文件进行解析。 我可以用找到mime-type,但是在调用之后,tika使用EmptyParser,检测到的内容类型是“application/octet-stream”。这是默认值,意味着tika无法找到它是什么类型的文件。我试图在解析文件之前设置元数据中的内容类型,但这导致

  • 问题内容: 我试图在春季连接一个messageSource以用于我的应用程序。它不起作用,给出此错误: org.springframework.context.NoSuchMessageException:在代码“ validation_required”下找不到语言环境“ en”的消息。 我的 applicationContext.xml 包含messageSource的以下定义: 我的邮件属性

  • 问题内容: 我正在eclipse中尝试以下代码: 但是它给我一个错误,说:列表不能解析为类型,而ArrayList不能解析为类型。 是否需要添加一些库,我该怎么做? 问题答案: 您可以按+ + 进行自动导入。

  • 问题内容: 我有一个基于Maven的GWT项目,其中包括Guava。我在尝试(失败)编译Maven时遇到麻烦: 我不知道为什么Maven认为需要在中编译源代码。这是我的项目的样子: SomeTestFile.java pom.xml 我已经尝试了以下方法: 删除依赖项(仅保留) 划定范围,以 我不确定还有什么尝试。包含源代码,因为GWT会将其编译为等效的Javascript。但是我不希望Maven