使用mime4j解析电子邮件



我在使用mime4j解析电子邮件时遇到了一个问题。这封电子邮件有一个附件,我使用MimeStreamParser来解析它。解析器根本不调用startMultipart方法。相反,它只调用一次body方法,并且BodyDescriptor是"text/plain"。

我不知道这个问题的根源,是电子邮件格式还是我的程序?

这是我的测试程序:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.james.mime4j.*;
import org.apache.james.mime4j.dom.BinaryBody;
import org.apache.james.mime4j.dom.Body;
import org.apache.james.mime4j.dom.Entity;
import org.apache.james.mime4j.dom.Header;
import org.apache.james.mime4j.dom.Message;
import org.apache.james.mime4j.dom.MessageBuilder;
import org.apache.james.mime4j.dom.Multipart;
import org.apache.james.mime4j.dom.TextBody;
import org.apache.james.mime4j.dom.address.Mailbox;
import org.apache.james.mime4j.dom.address.MailboxList;
import org.apache.james.mime4j.dom.field.AddressListField;
import org.apache.james.mime4j.dom.field.ContentTypeField;
import org.apache.james.mime4j.dom.field.DateTimeField;
import org.apache.james.mime4j.dom.field.UnstructuredField;
import org.apache.james.mime4j.field.address.AddressFormatter;
import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.MessageImpl;
import org.apache.james.mime4j.message.DefaultMessageBuilder;
import org.apache.james.mime4j.message.SimpleContentHandler;
import org.apache.james.mime4j.parser.ContentHandler;
import org.apache.james.mime4j.parser.MimeStreamParser;
import org.apache.james.mime4j.stream.BodyDescriptor;
import org.apache.james.mime4j.stream.Field;
import org.apache.james.mime4j.stream.MimeConfig;
public class TestClass extends SimpleContentHandler{
    public static void main(String[] args) throws MimeException, IOException {
        ContentHandler handler = new TestClass();
          MimeConfig config = new MimeConfig();
          MimeStreamParser parser = new MimeStreamParser(config);
          parser.setContentHandler(handler);
          InputStream instream = new FileInputStream("mail/testuser1");
          try {
              parser.parse(instream);
          } finally {
              instream.close();
          }
    }
    @Override
    public void headers(Header arg0) {
        // TODO Auto-generated method stub
        System.out.println("headers args: "+arg0);
    }
    @Override
    public void body(BodyDescriptor bd, InputStream is) {
        // TODO Auto-generated method stub
        System.out.println("body descriptor: "+bd);
    }
    public void startMessage(){
        System.out.println("startMessage");
    }

    public void endMessage(){
        System.out.println("endMessage");
    }

    public void startBodyPart(){
        System.out.println("startBodyPart");
    }

    public void endBodyPart() {
        System.out.println("endBodyPart");
    }


    public void preamble(InputStream is){
        System.out.println("preamble");
    }

    public void epilogue(InputStream is) {
        System.out.println("epilogue");
    }

    public void startMultipart(BodyDescriptor bd){
        System.out.println("startMultipart");
    }

    public void endMultipart() {
        System.out.println("endMultipart");
    }

    public void raw(InputStream is) {
        System.out.println("raw");
    }

}

这是我的电子邮件文件的一部分:

From MAILER_DAEMON  Wed Aug 21 19:24:53 2013
Date: Wed, 21 Aug 2013 19:24:53 +0800
From: Mail System Internal Data <MAILER-DAEMON@mail.abc.com>
Subject: DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA
Message-ID: <1377084293@mail.abc.com>
X-IMAP: 1377072167 0000000003
Status: RO
This text is part of the internal format of your mail folder, and is not
a real message.  It is created automatically by the mail system software.
If deleted, important folder data will be lost, and it will be re-created
with the data reset to initial values.
From testuser2@abc.com  Sat Aug 24 10:53:42 2013
Return-Path: <testuser2@abc.com>
X-Original-To: testuser1@abc.com
Delivered-To: testuser1@abc.com
Received: from shupc (unknown [192.168.75.130])
by mail.abc.com (Postfix) with SMTP id C0F5B1EFBC3
for <testuser1@abc.com>; Sat, 24 Aug 2013 10:53:42 +0800 (CST)
Message-ID: <7F1C30C9CB284CA594D01CBE210257D3@shupc>
From: "john" <testuser2@abc.com>
To: "smith" <testuser1@abc.com>
Subject: aaa
Date: Sat, 24 Aug 2013 10:53:42 +0800
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_000B_01CEA0B8.32903020"
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2900.5512
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5512
X-UID: 3                                                  
Status: O
Content-Length: 386430
This is a multi-part message in MIME format.
------=_NextPart_000_000B_01CEA0B8.32903020
Content-Type: multipart/alternative;
boundary="----=_NextPart_001_000C_01CEA0B8.32903020"

------=_NextPart_001_000C_01CEA0B8.32903020
Content-Type: text/plain;
charset="gb2312"
Content-Transfer-Encoding: base64
dGVzdCBhYSBiYiBjYw==
------=_NextPart_001_000C_01CEA0B8.32903020
Content-Type: text/html;
    charset="gb2312"
Content-Transfer-Encoding: base64
PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
L0VOIj4NCjxIVE1MPjxIRUFEPg0KPE1FVEEgaHR0cC1lcXVpdj1Db250ZW50LVR5cGUgY29udGVu
dD0idGV4dC9odG1sOyBjaGFyc2V0PWdiMjMxMiI+DQo8TUVUQSBjb250ZW50PSJNU0hUTUwgNi4w
MC4yOTAwLjU1MTIiIG5hbWU9R0VORVJBVE9SPg0KPFNUWUxFPjwvU1RZTEU+DQo8L0hFQUQ+DQo8
Qk9EWSBiZ0NvbG9yPSNmZmZmZmY+DQo8RElWPjxGT05UIHNpemU9Mj50ZXN0IGFhIGJiIGNjPC9G
T05UPjwvRElWPjwvQk9EWT48L0hUTUw+DQo=
------=_NextPart_001_000C_01CEA0B8.32903020--
------=_NextPart_000_000B_01CEA0B8.32903020
Content-Type: application/octet-stream;
    name="10112716229607.doc"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="10112716229607.doc"
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAAFAAAAKAIAAAAAAAAA
EAAAKgIAAAEAAAD+////AAAAACMCAAAkAgAAJQIAACYCAAAnAgAA////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////s
pcEAcWAJBAAA8FK/AAAAAAAAEAAAAAAABgAArJ0CAA4AYmpianFQcVAAAAAAAAAAAAAAAAAAAAAA
AAAECBYAOBIDABM6AQATOgEA1gwBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//w8AAAAA
AAAAAAD//w8AAAAAAAAAAAD//w

问题在于示例电子邮件不是多部分的。它以内联文本的形式包含一封由多部分组成的电子邮件。

删除第一个标题("FROM MAILER"),然后确保Content-Type后面的所有行都按照规范(RFC822或更高版本)的要求缩进至少一个空白字符(如字符集和边界),或者删除换行符。参见示例:

更改自:

Content-Type: multipart/mixed;
boundary="----=_NextPart_000_000B_01CEA0B8.32903020"

至任一:

Content-Type: multipart/mixed;
  boundary="----=_NextPart_000_000B_01CEA0B8.32903020"

或:

Content-Type: multipart/mixed; boundary="----=_NextPart_000_000B_01CEA0B8.32903020"

或者,尝试其他消息。

使用以下基于mime4j的库:电子邮件mime解析器

提供的示例代码负责电子邮件解析,生成的"电子邮件"对象为解决您的问题提供了方便的方法:

ContentHandler contentHandler = new CustomContentHandler();
MimeConfig mime4jParserConfig = new MimeConfig();
BodyDescriptorBuilder bodyDescriptorBuilder = new DefaultBodyDescriptorBuilder();
MimeStreamParser mime4jParser = new MimeStreamParser(mime4jParserConfig,DecodeMonitor.SILENT,bodyDescriptorBuilder);
mime4jParser.setContentDecoding(true);
mime4jParser.setContentHandler(contentHandler);

InputStream mailIn = 'Provide email mime stream here';
mime4jParser.parse(mailIn);
Email email = ((CustomContentHandler) contentHandler).getEmail();

相关内容

  • 没有找到相关文章

最新更新