[英]Parsing 'multipart/alternative' content-type
I am using javamail api to get unread messages from inbox folder, the problem is i am getting odd content-type 'multipart/alternative'
when i call Message.getContentType()
. 我正在使用javamail api从收件箱文件夹中获取未读消息,问题是当我调用Message.getContentType()
时,我收到了奇怪的内容类型'multipart/alternative'
。
Also when i down-cast content of message(from Object
) to class Multipart
i get an exception 当我将消息的内容(从Object
)下放到Multipart
类时,我Multipart
遇到异常
Exception in thread "main" java.lang.classCastException: com.sun.mail.imap.IMAPInputStream cannot be cast to javax.mail.Multipart at............
I just want to get the content of email and store it in DB. 我只想获取电子邮件的内容并将其存储在数据库中。
subject = messages[j].getSubject();
System.out.println(messages[j].getContentType());
if(messages[j].getContent() instanceof Multipart)
{
Multipart mime = (Multipart) messages[j].getContent();
for (int i = 0; i < mime.getCount(); i++)
{
BodyPart part = mime.getBodyPart(i);
content += part.getContent().toString();
}
}
Thanks. 谢谢。
Multiplart/alternative is not odd; 多重/替代不奇怪; in fact, it's very common. 实际上,这很常见。 It's typically used by email clients to create 2 versions of the same message, one that is plain text and the other that is HTML. 电子邮件客户端通常使用它来创建同一邮件的2个版本,一个是纯文本版本,另一个是HTML版本。 First, your email client must detect that the message is multipart/alternative, which it can do by finding these headers in the headers section: 首先,您的电子邮件客户端必须检测到邮件是多部分/替代邮件,可以通过在标头部分中找到以下标头来实现:
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=some-boundary
Second, it must parse each of the alternative body parts, examine their headers to see which one (or ones) that it wants to process, and then do so. 其次,它必须解析每个替代主体部分,检查其标头以查看要处理的一个或多个,然后再执行。
--some-boundary
Content-Type: text/plain
...The plain text version of the email goes here...
--some-boundary
Content-Type: text/html
<html>...The html version of the content goes here...</html>
--some-boundary--
I ran into similar problem while I was to read message attachments using Android JavaMail. 在使用Android JavaMail阅读邮件附件时遇到了类似的问题。 I have fixed this error by adding following lines of code. 我通过添加以下代码行来修复此错误。 There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. MailCap出了点问题,javamail找不到多部分/混合部分的处理程序,因此需要添加此位。 This resolved my problem. 这解决了我的问题。 Hope it helps someone out there. 希望它可以帮助某个人。
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
Cheers! 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.