如何用javax.mail解析邮件内容?

问题描述:

我想解析使用javax.mail的电子邮件。我想要获取文本内容和所有附件(最好是INLINE图片/附件)。如何用javax.mail解析邮件内容?

我有下面的代码,但它似乎打破了更复杂的邮件与几个嵌套multiparts。

我已阅读常见问题解答,并在整个一天内搜索不到解决方案。

请帮忙。

public static String fetchEmailcontent(Part message, String messageid) throws IOException, MessagingException { 

     StringWriter sw = new StringWriter(1024); 

     if (message != null && message.getContent() != null) { 
      if (message.getContent() instanceof Multipart) { 
       Multipart parts = (Multipart) message.getContent(); 
       BodyPart p; 
       boolean alternative = parts.getContentType().trim().toLowerCase().startsWith("multipart/alternative") ? true : false; 

       InputStreamReader isr; 
       int retrieved; 
       char[] buffer = new char[512]; 
       for (int i = 0; i < parts.getCount(); i++) { 
        p = parts.getBodyPart(i); 

        if (p.getContentType().toLowerCase().startsWith("multipart")) { 
         sw.write(fetchEmailcontent(p, messageid)); 
         break; 
        } else if ((Part.INLINE.equalsIgnoreCase(p.getDisposition()) || p.getDisposition() == null) && p.getContentType().toLowerCase().startsWith("text") && p.getFileName() == null) { 

         if (InputStream.class.isInstance(p.getContent())) { 
          InputStream ip = p.getInputStream(); 

          StringWriter subwriter = new StringWriter(ip.available()); 
          isr = new InputStreamReader(ip); 
          while (isr.ready()) { 
           retrieved = isr.read(buffer, 0, 512); 
           subwriter.write(buffer, 0, retrieved); 
          } 
          sw.write(subwriter.toString()); 
         } else { 
          Object content = p.getContent(); 
          if (java.io.ByteArrayInputStream.class.isInstance(content)) { 
           int bcount = ((java.io.ByteArrayInputStream) content).available(); 
           byte[] c = new byte[bcount]; 
           ((java.io.ByteArrayInputStream) content).read(c, 0, bcount); 
           sw.write(new String(c)); 
          } else { 
           sw.write(content.toString()); 
          } 
         } 
         if (alternative && !"".equals(sw.toString().trim())) { 
          break; 
         } 
         sw.write("\r\n"); 
        } else if (p.getDisposition() != null && (p.getDisposition().equalsIgnoreCase(Part.ATTACHMENT) || p.getDisposition().equalsIgnoreCase(Part.INLINE))) { 
         saveFile(MimeUtility.decodeText(p.getFileName()), p.getInputStream(), messageid); 
        } 
       } 
      } else if (message.getContentType().toLowerCase().startsWith("text")) { 
       sw.write(message.getContent().toString()); 
      } 
     } 
     return sw.toString(); 
    } 

这里有一个邮件的例子未能从获取附件: (我已删除页眉和使用Base64编码的附件,以节省空间..他们是完全正常的,否则)

Content-Type: multipart/mixed; boundary=f46d04016b4779522904c58fb5b4 

--f46d04016b4779522904c58fb5b4 
Content-Type: multipart/alternative; boundary=f46d04016b4779522104c58fb5b2 

--f46d04016b4779522104c58fb5b2 
Content-Type: text/plain; charset=ISO-8859-1 
Content-Transfer-Encoding: quoted-printable 

test 


sdljpjdpjsd 


=E5=E4=F6 

--f46d04016b4779522104c58fb5b2 
Content-Type: text/html; charset=ISO-8859-1 
Content-Transfer-Encoding: quoted-printable 

<div>test </div><div>=A0</div><div>=A0</div><div>sdljpjdpjsd</div><div>=A0<= 
/div><div>=A0</div><div>=E5=E4=F6</div> 

--f46d04016b4779522104c58fb5b2-- 
--f46d04016b4779522904c58fb5b4 
Content-Type: image/jpeg; name="blah.jpg" 
Content-Disposition: attachment; filename="blah.jpg" 
Content-Transfer-Encoding: base64 
X-Attachment-Id: f_h50rhk180 

BUNCH OF BASE64 
--f46d04016b4779522904c58fb5b4 
Content-Type: application/pdf; name="blah2.pdf" 
Content-Disposition: attachment; filename="blah2.pdf" 
Content-Transfer-Encoding: base64 
X-Attachment-Id: f_h50ria042 

BUNCH OF BASE64 
--f46d04016b4779522904c58fb5b4-- 

预期输出是邮件正文中的文本,并将所有附件保存到磁盘。 saveFile()函数可以做到这一点,但是它的基本功能我决定不包含它。我当然知道这不是罪魁祸首。

在此先感谢。

+0

您能否向我们提供应该显示信息的电子邮件?另外,解释它发现的附件有什么问题 – 2012-07-24 14:54:34

+0

我在原始文章中附加了电子邮件源。问题是它没有找到附件。 – 2012-07-24 14:57:24

在你的代码...

if (InputStream.class.isInstance(p.getContent()))可以是假的,但是, InputStream ip = p.getInputStream();仍然可以成功的!

希望这会有所帮助。

+0

我认为你的答案停在一半。它应该帮助什么?突出显示缺少返回值检查?如果是这样,就说出来;) – hakre 2012-09-26 08:19:22