Forum Stats

  • 3,782,553 Users
  • 2,254,664 Discussions
  • 7,880,121 Comments

Discussions

IMAPInputStream cannot be cast to javax.mail.Multipart

2607680
2607680 Member Posts: 2
edited Feb 12, 2014 9:55AM in JavaMail

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

...

2607680

Best Answer

  • Bill Shannon-Oracle
    Bill Shannon-Oracle Member Posts: 5,441 Bronze Badge
    Accepted 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:

    <span class="typ">MailcapCommandMap</span><span class="pln"> mc </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">MailcapCommandMap</span><span class="pun">)</span><span class="pln"> </span><span class="typ">CommandMap</span><span class="pun">.</span><span class="pln">getDefaultCommandMap</span><span class="pun">();</span><span class="pln"> <br/>mc</span><span class="pun">.</span><span class="pln">addMailcap</span><span class="pun">(</span><span class="str">"text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"</span><span class="pun">);</span><span class="pln"> <br/>mc</span><span class="pun">.</span><span class="pln">addMailcap</span><span class="pun">(</span><span class="str">"text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"</span><span class="pun">);</span><span class="pln"> <br/>mc</span><span class="pun">.</span><span class="pln">addMailcap</span><span class="pun">(</span><span class="str">"text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"</span><span class="pun">);</span><span class="pln"> <br/>mc</span><span class="pun">.</span><span class="pln">addMailcap</span><span class="pun">(</span><span class="str">"multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"</span><span class="pun">);</span><span class="pln"> <br/>mc</span><span class="pun">.</span><span class="pln">addMailcap</span><span class="pun">(</span><span class="str">"message/rfc822;; x-java-content- handler=com.sun.mail.handlers.message_rfc822"</span><span class="pun">);</span><span class="pln"> <br/></span><span class="typ"></span>

    2607680

Answers

  • Bill Shannon-Oracle
    Bill Shannon-Oracle Member Posts: 5,441 Bronze Badge
    Accepted 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:

    <span class="typ">MailcapCommandMap</span><span class="pln"> mc </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">MailcapCommandMap</span><span class="pun">)</span><span class="pln"> </span><span class="typ">CommandMap</span><span class="pun">.</span><span class="pln">getDefaultCommandMap</span><span class="pun">();</span><span class="pln"> <br/>mc</span><span class="pun">.</span><span class="pln">addMailcap</span><span class="pun">(</span><span class="str">"text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"</span><span class="pun">);</span><span class="pln"> <br/>mc</span><span class="pun">.</span><span class="pln">addMailcap</span><span class="pun">(</span><span class="str">"text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"</span><span class="pun">);</span><span class="pln"> <br/>mc</span><span class="pun">.</span><span class="pln">addMailcap</span><span class="pun">(</span><span class="str">"text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"</span><span class="pun">);</span><span class="pln"> <br/>mc</span><span class="pun">.</span><span class="pln">addMailcap</span><span class="pun">(</span><span class="str">"multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"</span><span class="pun">);</span><span class="pln"> <br/>mc</span><span class="pun">.</span><span class="pln">addMailcap</span><span class="pun">(</span><span class="str">"message/rfc822;; x-java-content- handler=com.sun.mail.handlers.message_rfc822"</span><span class="pun">);</span><span class="pln"> <br/></span><span class="typ"></span>

    2607680
  • 2607680
    2607680 Member Posts: 2

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

This discussion has been closed.