问题
In Java project, I can receive mails from gmail server. But I want to receive the package part of body. And in this code sample my last message(messages.length - 1)
is multipart/mixed.
Debug is pass on the if
block but it fall into the catch block and gave me this message:
Exception in thread "main" java.lang.ClassCastException: com.sun.mail.imap.IMAPInputStream cannot be cast to javax.mail.Multipart
How can I handle on this issue?
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 problem with using mail.jar, additional.jar and activation.jar which are using for only Java Project.
(I was download these jars before for my Android Project. That was the source of problem.)
回答1:
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!
回答2:
This usually happens because JAF can't find the configuration file that maps MIME types to classes. The most common reason for that failure is that the environment you're running in isn't setting the thread's context class loader correctly. See this JavaMail forum thread for a workaround.
The second most common reason for this failure is that you repackaged the classes in the mail.jar file into your own jar file, but forgot to include the configuration files in the META-INF directory.
回答3:
just curious, the type architecture shows they are from the different super class:
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.
来源:https://stackoverflow.com/questions/10302564/can-not-cast-imapinputstream-to-multipart