Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem attaching a File

 
Joe McGuire
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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};
msg.setRecipients(
Message.RecipientType.TO, address);
msg.setSubject( pSubject );
msg.setSentDate(new java.util.Date());
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(pMessage);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds=
new FileDataSource(pFileName);
mbp2.setDataHandler(
new DataHandler(fds));
mbp2.setFileName(pFileName);
// create the Multipart
//and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
Transport.send(msg);
 
Joe McGuire
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic