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()));
}
}
}
}
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.)
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With