简体   繁体   中英

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() .
Also when i down-cast content of message(from Object ) to class Multipart i get an exception

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. 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. 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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM