Search code examples
javajakarta-mailhtml-email

Can I add HTML tags to the email subject using JavaMail API?


I want to add the "white-space:nowrap" CSS property for certain two words in my email subject using JavaMail API.

Here is my code:

String str = "These <span style='white-space: nowrap'>Two words</span>";
mail.setSubject(str, "UTF-8");

But when I view in email client, subject has been displayed as text, not in HTML format.

These <span style='white-space: nowrap'>Two words</span>


Please let me know what could be the problem.


Solution

  • HTML is not possible in the subject. You might try a hard-space, also called non-breaking space, U+00A0.

    String str = "These Two\u00A0words";
    mail.setSubject(str ,"UTF-8");