File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes Problem attaching a File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Problem attaching a File" Watch "Problem attaching a File" New topic

Problem attaching a File

Joe McGuire
Ranch Hand

Joined: Mar 19, 2001
Posts: 293
I'm trying to use JavaMail to create an email message with an attachment. The problem is, that instead of getting an attached file, the email comes with the contents of the attachment displayed in the body of the message (the attachement is of an html file).
I've created a file object "htmlFile" which is made from a String of text ("<html><head><title>some file</title></head><body>some text</body></html>").
Here's the excerpt of code that puts together the message:

What do I need to change to make the html file an attachment, and not display in the body of the email document?
[This message has been edited by Joe McGuire (edited October 18, 2001).]
Judy Herilla
Ranch Hand

Joined: Jan 05, 2001
Posts: 89
Joe -
Sometimes there are email browsers which you set the default to display the content of the attachment. Could this be it?
Ive used this code before and it looks just like yours:
Message msg = new MimeMessage(session);
msg.setFrom( aDefaultFromAddress );
InternetAddress[] address = {aToAddress};
Message.RecipientType.TO, address);
msg.setSubject( pSubject );
msg.setSentDate(new java.util.Date());
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds=
new FileDataSource(pFileName);
new DataHandler(fds));
// create the Multipart
//and its parts to it
Multipart mp = new MimeMultipart();
// add the Multipart to the message

Joe McGuire
Ranch Hand

Joined: Mar 19, 2001
Posts: 293
I found the answer to my problem:
The trick is to set the Disposition of the MimeBodyPart object.
For the MimeBodyPart to be an attachment use:

For the MimeBodyPart to appear in the body of the email use:
I agree. Here's the link:
subject: Problem attaching a File
It's not a secret anymore!