JavaMail attachment functionality changes in JAR File.

Hi All,

I have developed a JavaMail Application using JBuilder4Pro. It works fine when I run it without creating a JAR (including attachments).

However if I package everything into a JAR JavaMail starts to work differently. I can still get all the message details but I can not see any attachments.

Can anyone help with this ?

Thanks,

** Phill **

[395 byte] By [pcam] at [2007-9-26 4:42:03]
# 1
Can you attach the code of java mail.
manian97 at 2007-6-29 18:04:53 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

The code for getting an attachment is shown bellow:

// Get the attachments

Multipart mp = (Multipart)oMailMessage.getContent();

int n = mp.getCount();

for(int j=0;j<n; j++) {

Part part = mp.getBodyPart(j);

String disposition = part.getDisposition();

if(disposition != null &&

(disposition.equals(Part.ATTACHMENT) ||

disposition.equals(Part.INLINE))) {

// Save the Attachment

saveAttachment(part.getFileName(), part.getInputStream());

}

The problem is with the line Multipart mp = (Multipart)oMailMessage.getContent();

It returns a SharedByteArrayInputStream instead of a MultiPart message when you

use it from a JAR file.

Again, this code works outside a JAR file.>

pcam at 2007-6-29 18:04:53 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
hi there......I M NABIL FROM ALGERIA I M A NEW USER DEVLPER JAVA SOFT & I WANNA USE IT TO DEVLOPE ANTI-CILLISION ROBOT SYSTEM IF YOU CAN HELP ME I `LL BE SO HAPPYAND IT IS WITH GREAT PLUSER TO WERK WITH YOU
nabil_asa at 2007-6-29 18:04:53 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
hi there......I M NABIL FROM ALGERIA I M A NEW USER DEVLPER JAVA SOFT & I WANNA USE IT TO DEVLOPE ANTI-CILLISION ROBOT SYSTEM IF YOU CAN HELP ME I `LL BE SO HAPPY GREATLY SEND ME TO :nabil_asa@eudoramail.com
nabil_asa at 2007-6-29 18:04:53 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

I have solved the problem. This problem can also occur outside a JAR file.

The solution

To ensure the classpath is correct use the command

java(w) YOURAPP.jar;mail.jar;activation.jar;pop3.jar YOURAPP

Instead of ( with a META file...)

java(w) -cp mail.jar;activation.jar;pop3.jar -jar YOURAPP.jar

Instead of ( without a META file...)

java(w) -cp mail.jar;activation.jar;pop3.jar -jar YOURAPP.jar YOURAPP

The Problem or Reason ( I Think )

When JAF tries to find the correct data handler (or data handler class) the classpath becomes an issue and only the underlying object not its datahandler is returned.

pcam at 2007-6-29 18:04:53 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

Phillip Cameron is right.

I had this problem when trying to deploy a JavaMail application using Java Web Start. Don't try to deploy the entire JWS application in one big JAR else you will have the JavaMail attachment problem. Instead, use separate JARs, your JNLP resourses section should look something like this:

<resources>

<j2se version="1.3"/>

<jar href="YOURAPP.jar" main="true"/>

<jar href="mail.jar"/>

<jar href="activation.jar"/>

<jar href="pop3.jar"/>

</resources>

kanemars at 2007-6-29 18:04:53 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

Hi Again,

I have also run accross a problem using UUEncoded Messages.

I seems that the files ALSO need to be included in the application JAR file to work. (Otherwise it does not find the DataHandler).

So to fix this I ALSO included the mail JAR files in the application JAR.

From,

Phillip Cameron

pcam at 2007-6-29 18:04:53 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...