Unable to enable IMAPInputStream in Multipart

In a Java project, I can receive mail from a gmail server. But I want to get a body part package. And in this code example, my last message(messages.length - 1) is compound / mixed.

Debug is passed to the if block but gets into the catch block and gives me the following message:

An exception in the "main" thread java.lang.ClassCastException: com.sun.mail.imap.IMAPInputStream cannot be cast to javax.mail.Multipart

How can I deal with this problem?

 Message[] messages = folder.getMessages(); ArrayList<String> attachments = new ArrayList<String>(); for (int i = messages.length - 1; i >= 0; i--) { Part p = messages[i]; if (messages[i].isMimeType("multipart/*")) { ***Multipart multipart = (Multipart) messages[i].getContent();*** for (int j = 0, m = multipart.getCount(); j < m; j++) { Part part = multipart.getBodyPart(j); String disposition = part.getDisposition(); // if (disposition != null && (disposition.equals("ATTACHMENT"))) { System.out.println(part.getFileName()); attachments.add(saveFile(MimeUtility.decodeText(part.getFileName()), part.getInputStream())); } } } } 

edit

I fixed the problem using mail.jar, Additional.jar and .jar activation, which are used only for Java Project .

(I downloaded these cans before for my Android project. That was the source of the problem.)

+2
source share
3 answers

I had a similar problem while I was reading message attachments using Android JavaMail. I fixed this error by adding the following lines of code. There is something wrong with MailCap, javamail cannot find a handler for the multi-part / mixed part, so this bit needs to be added. This solved my problem. Hope this 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); 

Hooray!

+10
source

This usually happens because JAF cannot find a configuration file that maps MIME types to classes. The most common reason for this failure is because the environment you are working in does not correctly install the thread context class loader. See this section of the JavaMail forum for a workaround.

The second most common reason for this failure is because you repackaged the classes in the mail.jar file to your own jar file, but forgot to include the configuration files in the META-INF directory.

+1
source

just curious, the type architecture shows that they are from another superclass:

public class IMAPInputStream extends java.io.InputStream {}

public abstract class Multipart {}

protected synchronized void Multipart :: setMultipartDataSource (MultipartDataSource mp);

while MultipartDataSource is defined as javax.mail.MultipartDataSource.

0
source

All Articles