Rohhit
Rohhit

Reputation: 742

how to make java string bold and colored

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.

Upvotes: 4

Views: 50517

Answers (3)

Ashwin Parmar
Ashwin Parmar

Reputation: 3045

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.

Upvotes: 1

sandymatt
sandymatt

Reputation: 5612

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

Upvotes: 1

Petr Mensik
Petr Mensik

Reputation: 27526

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");`

Upvotes: 5

Related Questions