1

I have to copy some text from a plain text file into Gmail's mail composer and make some parts of that text bold. I can obviously boldface those specific parts manually after pasting it to the composer. But is there a way to do this automatically such that when I copy that content to the Gmail composer those parts is already bold.

For example, here's the plain text to be copied to Gmail:

Breaking News: Elon Musk just invaded Mars.

I would normally copy this text to Gmail's composer and manually bolden the part Breaking news. But if the source would have been, say, MS Word file instead of a plain text file, Gmail would have boldfaced it for me.

Background: The reason I am looking for this is that the plain text file is an output of a simple Java program and I would just edit the Java program to give the necessary output that would handle boldfacing.

P.S: I have tried wrapping those parts with <b> tags like this:

<b>Breaking news </b>: Elon Musk just invaded Mars.

It didn't work. Gmail simply prints the tag itself with my content.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Sagar
  • 3,107
  • 2
  • 26
  • 35
  • anwered here I guess https://stackoverflow.com/questions/29109678/java-print-in-bold – so_what Jan 20 '18 at 18:39
  • @so_what not really. I doubt Gmail cares about ANSI escape sequences, but if you're creating a HTML email, then you can format it in lots of different ways. – Kayaman Jan 20 '18 at 18:42
  • Can you post the code you're using to send the message to the Gmail composer? So that we can know if that will accept HTML tags such as `` – Jules Dupont Jan 20 '18 at 18:44
  • @so_what That question speaks about printing in bold. My case is writing to a file in bold. – Sagar Jan 20 '18 at 18:59
  • @JulesDupont I don't use any code to send the message to Gmail composer. It's just a manual copy-paste. – Sagar Jan 20 '18 at 19:00
  • 2
    So your question actually has nothing to do with Java or file IO. It's just about having Gmail accept HTML. – Kayaman Jan 20 '18 at 19:12
  • Could you please provide (post) a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)? – Sergey Vyacheslavovich Brunov Jan 20 '18 at 23:46
  • Thanks for the feedback @Kayaman and SergeyBrunov, I have updated my question! – Sagar Jan 21 '18 at 13:11
  • 1
    What if you copy the text from a browser with the rendered HTML? If you're just copying the raw text, it'll be of type "text". Gmail doesn't know that you want it to interpret the text as HTML. – Kayaman Jan 21 '18 at 13:18
  • @Kayaman Yeah, that's a smart alternative I guess. Thank you! – Sagar Jan 21 '18 at 21:24

1 Answers1

1

The Sending Email | Gmail API | Google Developers example may be used as the starting point with the addition, which is described below.

It is enough to prepare the message body (the value of the htmlBody variable) as the HTML document:

final MimeMessage message = new MimeMessage(session);

// From.
message.setFrom(...);

// To.
message.addRecipient(...);

// Subject.
message.setSubject(...);

// Body: HTML content.
message.setContent(htmlBody, "text/html");

Basic information on the HTML document formatting, in particular, on how to make a piece of text bold, please refer to HTML Text Formatting: see the <b> element.