Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Problem attaching a File

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic