Reputation: 1139
I am using JavaMail library, I would like to change the body of the emails, sentences in different color? How can I do it? My application is in (Swing/JFrame)
Upvotes: 3
Views: 11578
Reputation: 3108
For me this worked flawlessly, Worth a shot :
String htmlText2 = "<font color=red>Jon Targaryen</font>\n";
or if you want to use hex color :
String htmlText2 = "<font color=#93cff2>Jon Targaryen</font>\n";
You can add more attributes like Headings or Bold :
String htmlText2 = "<H1><font color=red>Jon Targaryen</font></H1>\n";
String htmlText2 = "<b><H1><font color=red>Jon Targaryen</font></H1></b>\n";
Upvotes: 0
Reputation: 1896
An example of sending email as HTML: http://www.tutorialspoint.com/java/java_sending_email.htm
What Baadshah is suggesting is adding all of your color formatting inside the Content string using html tags.
message.setContent("<h1>This is actual message</h1>",
"text/html" );
You can programatically construct the string that contains the body message.
String line1 = "This is the first line in the body. We want it to be blue."
addColor(line1, Color.BLUE);
Then create a method for handling the colorizing html:
public static String addColor(String msg, Color color) {
String hexColor = String.format("#%06X", (0xFFFFFF & color.getRGB()));
String colorMsg = "<FONT COLOR=\"#" + hexColor + "\">" + msg + "</FONT>";
return colorMsg;
}
You can examine different ways of colorizing in HTML here: http://www.htmlgoodies.com/tutorials/colors/article.php/3479011/How-To-Change-Text-Color-Using-HTML-and-CSS.htm. This includes old ways of doing it, like using FONT (as my example above) or modern ways of doing it using CSS.
Edit: The toHexString returns an 8 Character hex code (alpha + red + blue + green) while HTML only wants the RGB without alpha. I used the solution from this link, and setup a SSCCE:
import java.awt.Color;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class EmailTestHTML
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
// Assuming you are sending email from localhost
String host = "putYourSMTPHostHere";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
// String with body Text
String bodyText = addColor("This line is red.", Color.RED);
bodyText += "<br>" + addColor("This line is blue.", Color.BLUE);
bodyText += "<br>" + addColor("This line is black.", Color.BLACK);
System.out.println(bodyText);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Send the actual HTML message, as big as you like
message.setContent(bodyText,
"text/html" );
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
public static String addColor(String msg, Color color) {
String hexColor = String.format("#%06X", (0xFFFFFF & color.getRGB()));
String colorMsg = "<FONT COLOR=\"#" + hexColor + "\">" + msg + "</FONT>";
return colorMsg;
}
}
Note: In my environment I had to set this argument in the Run Configuration:
-Djava.net.preferIPv4Stack=true
More on that here.
Upvotes: 6
Reputation: 122018
Its just css.
Nothing to do with JAVA
.The browser detects your HTML content which you are sending in email
.
For example
<div style="font-size:14px">Dear user</div>
Upvotes: 3
Reputation: 6226
You have to send the mail in HTML format to be able to change text color.
See the JavaMail FAQ.
Upvotes: 1