File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes With JavaMail is it possible to add html Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "With JavaMail is it possible to add html " Watch "With JavaMail is it possible to add html " New topic

With JavaMail is it possible to add html

bobby, morkos
Ranch Hand

Joined: Jan 04, 2002
Posts: 82
For message I want to add html instead of String
msg.setText ( message );
Is it possible and how do I modify my code below to take into account these changes. It would be better if I can point to an html page template [template.html] and automatically setText for that html template.
I have the following code:
Properties properties = new Properties ( );
properties.put ( "", mail_server_host );
MimeMessage msg = new MimeMessage (
Session.getInstance ( properties, authenticator ) );
msg.addFrom ( addresses_from );
msg.setRecipients ( Message.RecipientType.TO , addresses_to );
msg.setSubject ( subject );
// for message I want to add html problem here, is there a way to change String into HTML
msg.setText ( message );
// set the Date: header
msg.setSentDate(new Date());
// add attachment
// 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(filePath);
mbp2.setDataHandler( new DataHandler(fds));
mbp2.setFileName( fileName );

Multipart mp = new MimeMultipart();
// add the Multipart to the message
Transport.send ( msg );
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Maybe you could try creating a string that includes HTML. I know you can use it for the String Text in a JLabel and JButton, etc.
Something like:
String htmlString = "<HTML>html code here</HTML>";
I haven't tried it, but let me know if it works or not.

GenRocket - Experts at Building Test Data
I agree. Here's the link:
subject: With JavaMail is it possible to add html
It's not a secret anymore!