• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

With JavaMail is it possible to add html

 
bobby, morkos
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.smtp.host", 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();
mbp1.setText(message);
// 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();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
Transport.send ( msg );
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic