Skip to main content

multipart/alternative

  15 posts   Feedicon  
Replies: 14 - Last Post: December 11, 2012 21:37
by: Mardolf
showing 1 - 15 of 15
Posted: July 05, 2011 16:35 by dwhytock
Hi all...

Trying to handle messages coming in with a content-type of "multipart/alternative". What I get from Message.getContent() is a javax.mail.util.SharedByteArrayInputStream, where according to the javadoc I should be getting a javax.mail.Multipart.

Is "multipart/alternative" handled by the current API?

Thanks...

Don
Posted: July 05, 2011 17:34 by Bill Shannon
Yes, it's handled.

There's probably something wrong in your setup that is causing this to fail.

Do regular multipart/mixed messages work as expected?

What environment is your code running in? A standalone Java application? An application server?

What version of JavaMail are you using, and what version of the JDK are you using?
Posted: July 05, 2011 18:07 by dwhytock
I haven't been able to get regular attachments either.

My code is in an OSGi bundle, running under Apache Felix 3.0.8.

I'm using JavaMail 1.4.4, running with Java 1.6.0_06.
Posted: July 05, 2011 18:09 by dwhytock
BTW, I'm able to receive "text/plain" messages with no problem.
Posted: July 05, 2011 20:36 by dwhytock
Okay, I tried a plain application structured as an OSGi bundle. Running it standalone I see MimeMultipart. Running it in Felix I see SharedByteArrayInputStream.

The mailapi bundle doesn't complain on startup, so I assume it's able to find javax.activation.

Is there maybe a configuration file, something in javaee.jar or mailapi.jar, that DataHandler can't get to in an OSGi environment?
Posted: July 06, 2011 04:48 by Bill Shannon
Sigh. It's more complicated than that, and I haven't had time to implement the solution.

It's all about ClassLoaders and the assumptions that OSGi breaks...

JavaMail (and JAF, which is the culprit in this case) uses the ClassLoader.getResource
method to load configuration files that are contained in the JavaMail jar file. With OSGi,
that doesn't work. So all the configuration JAF needs to find the correct class to handle
certain MIME types is missing.

You can try using Thread.setContextClassLoader to set it to the ClassLoader of your
application bundle. If that doesn't work, a more complicated workaround may be
required.
Posted: October 17, 2012 04:47 by Codisimus
Could you explain what "a more complicated workaround" would include? I have this issue in a project I am working on, setting the ClassLoader fixes it for me but the same jar still does not work for other users.
Posted: October 17, 2012 20:35 by Bill Shannon
You can use CommandMap.getDefaultCommandMap(), cast it to a MailcapCommandMap, and use the addMailcap() method to add the entries that would've been found had the class loader found the correct configuration file. You can find the entries in the mailcap file in the mail.jar file.

Does that help?
Posted: October 24, 2012 22:07 by Codisimus
I tried that but still get "SEVERE java.lang.ClassCastException: javax.mail.util.SharedByteArrayInputStream cannot be cast to javax.mail.Multipart"

perhaps I am doing it wrong, here is my code:

Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
MailcapCommandMap cMap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
cMap.addMailcap("text/plain; ; x-java-content-handler=com.sun.mail.handlers.text_plain");
cMap.addMailcap("text/html; ; x-java-content-handler=com.sun.mail.handlers.text_html");
cMap.addMailcap("text/xml; ; x-java-content-handler=com.sun.mail.handlers.text_xml");
cMap.addMailcap("multipart/*; ; x-java-content-handler=com.sun.mail.handlers.multipart_mixed; x-java-fallback-entry=true");
cMap.addMailcap("message/rfc822; ; x-java-content-handler=com.sun.mail.handlers.message_rfc822");

I wouldn't think the first line is needed but it doesn't work without it. (on the other setup it does not work either way)
Thanks again.
Posted: October 25, 2012 04:00 by Bill Shannon
Set the System property "javax.activation.debug" to "true".
The debug output to System.out might help us figure out
what's going wrong.
Posted: October 27, 2012 04:44 by Codisimus
Ok I have the debug output for my machine (which works) and a machine hosted elsewhere (which doesn't work). Here is the debug info: http://pastie.org/5122095

If more information about the host would be helpful I could proly get it. It looks like I wasn't even allowed to set the System property because there is no output (just the ClassCastException that is thrown).

Thanks for all the help so far.
Posted: October 27, 2012 06:00 by Bill Shannon
If you weren't allowed to set the System property, you would've gotten
a SecurityException.

My best guess is that you're not actually using JDK 1.6 on the machine
that doesn't work. Otherwise, I can't explain why you're not getting the
debug output that you get on the working machine.

Double check the configuration of both machines, make sure they're
using the same jar files, JDK, etc.
Posted: October 27, 2012 22:09 by Codisimus
I'm not sure why it didn't show up then, I guess the debug messages are showing up now and I am trying to get a full log of them from him. This is all he showed me so far: (when trying to read a multipart)

27.10 00:04:01 [Server] SEVERE START DEBUG:
27.10 00:04:01 [Server] INFO MailcapCommandMap: load HOME
27.10 00:04:01 [Server] INFO new MailcapFile: file /home/minecraft/multicraft/servers/server382/.mailcap
27.10 00:04:01 [Server] INFO MailcapCommandMap: load SYS
27.10 00:04:01 [Server] INFO new MailcapFile: file /usr/lib/jvm/jdk1.7.0/jre/lib/mailcap
27.10 00:04:01 [Server] INFO MailcapCommandMap: load JAR
27.10 00:04:01 [Server] INFO MailcapCommandMap: getResources
27.10 00:04:01 [Server] INFO MailcapCommandMap: URL jar:fileHmmmhome/minecraft/multicraft/servers/server382/lib/mail.jar!/META-INF/mailcap
27.10 00:04:01 [Server] INFO new MailcapFile: InputStream

Like I said I will get the rest of the debug output later. Also you can see that it is JDK 1.7
Posted: November 25, 2012 22:33 by Codisimus
I eventually fixed this. It's more like a work around but I might as well post it here in case it can help someone else.

To get the Multipart object I did the following:

DataSource source = new ByteArrayDataSource(part.getInputStream(), MULTIPART_TYPE);
Multipart mp = new MimeMultipart(source);
Posted: December 11, 2012 21:37 by Mardolf
I only registered here to thank you Codisimus, your solution and posts solved this problem i also had. In my case i was using Eclipse and JDK 1.6 and Javamail 1.4.5, moreover i decided to add the source code of javamail instead of the jar. It was a simple mail client project. I would like to add that i just replaced the part "MULTIPART_TYPE" for "multipart/*"
Thx again.

showing 1 - 15 of 15
Replies: 14 - Last Post: December 11, 2012 21:37
by: Mardolf
 
 
Close
loading
Please Confirm
Close