Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

IMAPInputStream cannot be cast to javax.mail.Multipart

2607680Feb 7 2014 — edited Feb 12 2014

Hello,

wonder if anyone could suggest how to resolve this.

Environment:

windows 7

Jre 1.7.0_13

javamail 1.5

I'm having problem with a message "part". When I execute a simple java class in Eclipse , and cast "part" to multipart

     Multipart mp = (Multipart) p.getContent();

but when I run the same class (same jars) from a database event (starts the same jvm) it reports :

Exception in thread "main" java.lang.classCastException: com.sun.mail.imap.IMAPInputStream cannot be cast to javax.mail.Multipart at

So, when I was debugging this , I can see that when running with Eclipse , instanceof "part" is reported as Multipart, but when running from a database as InputStream.

I've read that this might be related to DataHandler ?? but I couldn't figure out how ? and what should I do to resolve it. Wonder if anyone would have some suggestions?

some snippet:

...

// Get system properties

        Properties properties = System.getProperties();

        // Get the default Session object.

        Session session = Session.getDefaultInstance(properties);

        session.setDebug(debug);

        // Get a Store object that implements the specified protocol.

        Store store = session.getStore(protocol); // gmail setting

        //Connect to the current host using the specified username and password.

        store.connect(host, user, password);

        //Create a Folder object corresponding to the given name.

        Folder folder = store.getFolder(mbox);

        // Open the Folder.//

        folder.open(Folder.READ_WRITE);

     

        FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);     

        Message[] message = folder.search(ft);

        for (i = 0; i < message.length; i++) {     

       alert_sent = null;

            java.util.Date msg_sent_date = message[i].getSentDate();

            alert_sent = new java.sql.Timestamp(msg_sent_date.getTime());

            alert_from = "";

            alert_from = InternetAddress.toString(message[i].getFrom()); 

            alert_subject = "";

            alert_subject = message[i].getSubject();

            content_type = "";

       content_type = message[i].getContentType();             

      

       alert_text = "";

       alert_html = "";

    if ( content_type.startsWith("text/") || content_type.startsWith("TEXT/") )

    { alert_text =  message[i].getContent().toString();}

    else

    {

    alert_html = getMessageText(message[i]);

    }

  

.........

and in getMessageText(Part p);

...

         Multipart mp = (Multipart) p.getContent();

...

This post has been answered by Bill Shannon-Oracle on Feb 7 2014
Jump to Answer

Comments

Bill Shannon-Oracle
Answer

It sounds like you're running your Java code "in" the database.  JavaMail depends on some configuration files to map MIME types to Java classes (e.g., "maultipart/mixed" to "javax.mail.internet.MimeMultipart").  These configuration files are loaded using the ClassLoader for the application.  If the ClassLoader doesn't function properly, these configuration files won't be found.

I vaguely remember hearing about such a problem in the Oracle database.  You should contact support to find out if this is a known problem and find out if a fix is available.

As a workaround, you can try adding the following to your application:

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");

Marked as Answer by 2607680 · Sep 27 2020
2607680

Thanks. Once I plugged your code it works like a charm. Thanks.

1 - 2
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 12 2014
Added on Feb 7 2014
2 comments
9,584 views