Got empty attachment when sending email using JavaMail Multipart

I have problem sending an email using JavaMail Multipart. Here is the piece of code:

Multipart mp = new MimeMultipart();

// create and fill the first message part - email content

MimeBodyPart mbp1 = new MimeBodyPart();

mbp1.setText("email content");

mp.addBodyPart(mbp1);

// create the second message part - an attachment

MimeBodyPart mbp2 = new MimeBodyPart();

FileDataSource fds = new FileDataSource("C:/file.txt");

mbp2.setDataHandler(newDataHandler(fds));

mbp2.setFileName(fds.getName());

mp.addBodyPart(mbp2);

// add the Multipart to the message

msg.setContent(mp);

Transport.send(msg);

The email gets send successfully. However, the email content is not seen and the attachment is empty.

I have turned on the javamail debug, there only one part and nothing is displayed in that part. Following is what I got from debug:

==============================================

.....

354 End data with <CR><LF>.<CR><LF>

Message-ID: <3577952.1165299176687.JavaMail.javamailuser@localhost>

From: John Doe <jdoe@gmail.com>

To: Someone <someone@yahoo.com>

Subject: Email Test

Mime-Version: 1.0

Content-Type: multipart/mixed; boundary="-=_Part_0_2388206.1165299176593"

.

250 Ok: queued as C5BB7A743

==================================================

If I write an standalong application (one JSP and this Java class), it works fine. I can see my email content and my attachment. However, when I plug in to my application, the email content and attachment are messed up. I cannot figure out which setting causes this problem. Any thought will be appreciated.

[1773 byte] By [ktsaoa] at [2007-10-3 11:37:30]
# 1

I don't see anything obviously wrong in the code you included.

And since it works for you in some environments, it's most likely

a configuration problem of some sort.

Are you running your app in a web server or app server that might

have an older version of JavaMail?

If so, you might need to add msg.saveChanges() before the call to

Transport.send.

Oh, and be sure the file you're attaching really exists on the server,

and that you're not getting any exceptions from Transport.send.

bshannona at 2007-7-15 14:05:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Thanks for you suggestion. However, the msg.saveChanges() doesn't help.....

I agree with you that there should be something wrong with configuration. However, I cannot find it. Do you know which setting(s) might cause this issue? Any suggestion I would willing to try.

By the way, the file exists and there is no exceptions.

ktsaoa at 2007-7-15 14:05:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

If you replace Transport.send with

msg.saveChanges();

msg.writeTo(new FileOutputStream("msg.txt"));

do you get the correct data in the msg.txt file?

I can think of a lot of things that might go wrong, but I

can't explain why any of them would cause the behavior

you're seeing. Still, one problem that often causes strange

behavior is having more than one version of the javax.mail

classes available to the application, e.g., by having two

copies of mail.jar in the CLASSPATH. You might check for

things like that.

bshannona at 2007-7-15 14:05:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
Thank you soooo much! I got it working!Yes. I have two copies of mail.jar in my CLASSPATH. I really appreciate your help.
ktsaoa at 2007-7-15 14:05:54 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...