3

I am developing email application using java. I have a string as follows :

String msg= "College Email \nHello"+" "+name+"\n"+"You are selected As 'Admin'.\n Please use Given username and password for login\n \Username:"+" "+username+" "+"Password:"+" "+password+"";

I want send this string as email message. and i want to "BOLD and color" username and password(I want to show this message in inbox i.e on browser). how can i embed HTML tags to do this ? OR is there any simple way to do this without using HTML tags ?

Can any one correct above string (msg) OR provide any link or code to fix this problem ?

thank you.

Rohhit
  • 722
  • 3
  • 12
  • 25

3 Answers3

4

Just type the tags into your message like String msg = "<strong>Hello World!</strong> and send it as a HTML message, you only need to set type of the content via MimeMessage#setContent() method like

message.setContent(msg, "text/html; charset=utf-8");`
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • hi petr ...how can i instantiate MimeMessage ?? is it like this : MimeNessage message =new MimeMessage(arg0); message.setContent(msg,"....") Actully arg0 is required as argument.. How can i do this ?? – Rohhit Oct 15 '13 at 08:06
  • Just do `Message msg = new MimeMessage(yourSession)`; – Petr Mensik Oct 15 '13 at 08:09
1

If you want to do it the old school, vanilla java way you can use the org.w3c.dom.Document API to get the work done.

It has a bunch of useful methods for creating and manipulating elements such as createElement, createAttribute, etc. to do what you want.

http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Document.html

yamafontes
  • 5,552
  • 1
  • 18
  • 18
1

As per the Javadoc, the MimeMessage#setText() sets a default mime type of text/plain, while you need text/html. Rather use MimeMessage#setContent() instead.

String someHtmlMessage = "Hello this is test message <b style='color:blue;'>bold color</b>";

message.setContent(someHtmlMessage, "text/html; charset=utf-8");

Note that the HTML should not contain the <html>, <head> or <body>. Gmail will ignore it. See also CSS support in mail clients.

Ashwin Parmar
  • 3,025
  • 3
  • 26
  • 42