2026-07-15

how to make java string bold and colored

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

how to make java string bold and colored

Mastering Email Formatting in Java: How to Make Strings Bold and Colored

As a senior developer, I often encounter scenarios where data needs to be transmitted, but its presentation within the recipient's inbox requires specific styling. Your request perfectly illustrates this challenge: taking a plain Java String and transforming it into a rich, formatted HTML message.

The core issue is understanding the separation between data (what you store in Java) and presentation (how the data is displayed in an email client or browser).

Here is a comprehensive guide on how to correctly embed HTML tags into your Java strings to achieve bolding and coloring for email communication, along with best practices.


The Challenge: Plain Strings vs. Rich Content

Your initial string:

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+";

If you send this as a plain text email, the formatting will be lost. To display bold or colored text in an HTML-enabled email client (like Gmail or Outlook), you must embed standard HTML tags within your string.

The simple answer is: You must construct the message as HTML, not just raw text.

Solution: Embedding HTML Tags in Java

To achieve bold and color, we will use standard HTML tags like <b> for bolding and <font color="..."> or the modern <span> with CSS for coloring. Since email clients can be inconsistent regarding CSS support, using older, more universally supported inline styles is often safer for maximum compatibility.

Let's correct your string construction by injecting these tags around the sensitive information (username and password).

Corrected Java Implementation Example

Instead of concatenating simple strings, we will use placeholders to insert HTML structure:

public class EmailFormatter {
    public static void main(String[] args) {
        String name = "Jane Doe";
        String username = "jdoe123";
        String password = "SecureP@ss123";

        // Constructing the message using HTML tags for formatting
        String htmlMessage = 
            "<html><body>" +
            "<p>College Email</p>" +
            "<p>Hello, " + name + "!</p>" +
            "<p>You are selected As 'Admin'.</p>" +
            "<p>Please use the following credentials for login:</p>" +
            "<b>Username:</b> <span style='color: blue; font-weight: bold;'>" + username + "</span><br>" +
            "<b>Password:</b> <span style='color: red; font-weight: bold;'>" + password + "</span>" +
            "<p>Thank you.</p>" +
            "</body></html>";

        System.out.println(htmlMessage);
    }
}

Explanation of the Code

  1. HTML Shell: We wrap the entire content in <html><body>...</body></html>. This tells the email client that the content is structured HTML, not plain text.
  2. Bolding (<b> or <strong>): Used for key labels like "Username:" and "Password:".
  3. Coloring (Inline CSS): We use the <span> tag combined with inline CSS (style='color: blue; font-weight: bold;'). Inline CSS is generally preferred in email development because many older email clients strip out external stylesheets, making inline styles more reliable for cross-client compatibility.
  4. Line Breaks: Using <br> (line break) allows us to structure the flow of the text exactly as desired within the HTML context.

Best Practices and Developer Insights

Security Note: Handling Passwords

As a senior developer, I must emphasize a critical point: Never transmit actual passwords in plain text, even inside an email. The example above demonstrates formatting, but for real-world applications, you should be using secure hashing (like BCrypt) and never storing or transmitting raw credentials. If this data is for demonstration purposes only, ensure it is handled with extreme care.

Separation of Concerns

Notice how we separated the logic. The Java code handles string construction, but the styling logic (the colors and fonts) is embedded directly into the string as HTML. In large applications, a better approach is often to use a dedicated Template Engine (like Thymeleaf or FreeMarker) on the backend to generate this complex HTML structure, keeping your core Java business logic clean. This aligns with modern architectural patterns, similar to how robust frameworks like those found in the Laravel ecosystem manage view rendering efficiently.

Conclusion

To successfully make a Java string display bold and colored in an email inbox, you must treat that string not as plain text, but as HTML markup. By strategically embedding tags (<b>, <span>) and inline styling into your message construction process, you gain control over the presentation layer. While this requires careful handling of HTML syntax, it is the standard method for achieving rich communication in email systems.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.