Email attachment parsing via mime4j

余生颓废 提交于 2019-12-04 07:59:21

Mime4j can definitely do what you need. Make a subclass of org.apache.james.mime4j.message.SimpleContentHandler, and implement the bodyDecoded method. Then pass this subclass into a MimeStreamParser instance. You'll see that bodyDecoded is called once per body or attachment, and the stream it gives you contains the data with Base64 or Quoted-Printable decoding already performed.

If you don't like event-based APIs in general, you can try the more DOM-like functionality exposed via the org.apache.james.mime4j.message.Message class. You can call Message.getBody() and then see if what it gives you back is an instance of Multipart, TextBody, BinaryBody, etc. If Multipart, then you can call Multipart.getBodyParts to recurse into the subparts.

@Ashish the other option since you are using SubEthaSmtp is to do what SubEtha Mail List manager does: http://code.google.com/p/subetha/source/browse/trunk/src/org/subethamail/common/SubEthaMessage.java

The linked class extends SMTPMessage and which has a bunch of getPart() methods. javax.mail.Part has most of what you need to turn an attachment into a file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!