vijay vijay - 10 months ago 50
Java Question

Change String colour JavaMail

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)

Answer Source

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 = "targetemail@somehost.com";

   // Sender's email ID needs to be mentioned
   String from = "youremail@somehost.com";

   // 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download