简体   繁体   English

解析“多部分/替代”内容类型

[英]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--

You're probably running into the same problem described here . 您可能会遇到这里描述的相同问题。

Note that multipart/alternative is a perfectly normal type, as described here . 注意,多部分/替代是一种完全正常的类型,如所描述这里

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.

相关问题 Multipart,设置一部分的 Content-Type - Multipart, set Content-Type of one part "Quarkus-resteasy-multipart 找不到内容类型多部分\/表单数据类型的编写器" - Quarkus-resteasy-multipart could not find writer for content-type multipart/form-data type 在 Java 中解析 Content-Type 标头而不验证字符集 - Parsing a Content-Type header in Java without validating the charset 在java中解析Multipart /与Multipart / Alternative主体混合 - Parsing Multipart/Mixed with Multipart/Alternative body in java spring rest webservices使用application / json content-type返回multipart / form-data - spring rest webservices return multipart/form-data with application/json content-type 更新 Android 中多部分表单数据请求的默认内容类型 - Update default Content-Type for multipart form-data request in Android 如何在 RestAssured 的正文中发送带有表单数据的 POST,并设置具有特定内容类型的多部分 - How to send a POST with form-data in Body in RestAssured setting multipart with a specific content-type 多部分/表单数据之间的空白问题; &amp; 内容类型标题中的边界 - White-space issue between multipart/form-data; & boundary in Content-Type Header org.glassfish.jersey.message.internal.HeaderValueException:无法解析“Content-Type”标头值:“multipart/byteranges” - org.glassfish.jersey.message.internal.HeaderValueException: Unable to parse “Content-Type” header value: “multipart/byteranges” 如何在使用RestTemplate(来自其他客户端)时为分段上传中的文件设置内容类型 - How to set content-type for the file in multipart upload when using RestTemplate (from a rest client)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM