通过mime4j解析电子邮件附件 [英] Email attachment parsing via mime4j

查看:247
本文介绍了通过mime4j解析电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个小型的java smtp库( http://code.google.com/p / subethasmtp / ),我需要在单独的组件中解析传入的电子邮件,即正文,附件等。

I am using a small java smtp library (http://code.google.com/p/subethasmtp/), by this I need to parse the incoming emails in separate components viz body, attachments etc.

我正在尝试使用mime4j,但是文档表明mime4j只能提供事件通知或令牌通知,而不是其他任何内容。为了剥离正文和附件等,我必须在事件处理程序中使用我自己的自定义逻辑。我的观察是否正确?

I am trying to use mime4j , but the documentation suggests that mime4j can only give event notification or token notification and nothing else. For stripping out body and attachments etc I had to use my own custom logic inside the event handlers. Is my observation correct?

如果是,那么我如何使用mime4j来满足我的要求。请建议。

If yes then how can I use mime4j to use for my requirement. Please suggest.

我非常需要一种接收smtp数据流的方法,并在java中以完全解析的形式返回一系列附件引用或流。请帮忙。

I badly need an approach that takes in the smtp data stream and returns me with an array of attachment references or streams in fully parsed out form in java. Please help.

提前致谢

Ashish Sharma

Ashish Sharma

推荐答案

Mime4j绝对可以满足您的需求。创建org.apache.james.mime4j.message.SimpleContentHandler的子类,并实现bodyDecoded方法。然后将此子类传递给MimeStreamParser实例。你会看到bodyDecoded每个主体或附件被调用一次,它给你的流包含已经执行过Base64或Quoted-Printable解码的数据。

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.

如果你通常不喜欢基于事件的API,您可以尝试通过org.apache.james.mime4j.message.Message类公开的更多类似DOM的功能。你可以调用Message.getBody()然后查看它给你的回复是Multipart,TextBody,BinaryBody等的实例。如果是Multipart,那么你可以调用Multipart.getBodyParts来递归到子部分。

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.

这篇关于通过mime4j解析电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆