4

My email content text is

String text = "Hey, You are create a new Account! Best, MyTeam";

How can I format the text to:

Hey,

You are create a new Account!

Best, MyTeam
skaffman
  • 398,947
  • 96
  • 818
  • 769
tomasz-mer
  • 3,753
  • 10
  • 50
  • 69

2 Answers2

5

SimpleMailMessage can only handle plain-text messages, so you need to embed explicit line-breaks in your String:

String text = "Hey,\n\nYou are create a new Account!\n\nBest, MyTeam";

A templating system such as velocity or freemarker may make this easier.

If you want to handle HTML messages, you need to use JavaMailSender and MimeMessagePreparator.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Microsoft Outlook removes extra line breaks so if you are using Outlook, the above approach might not work perfectly. In that case you can refer [this answer](https://stackoverflow.com/questions/136052/how-do-i-format-a-string-in-an-email-so-outlook-will-print-the-line-breaks) to solve your problem. – Gaurav Parek May 13 '20 at 12:21
0

Try

String text = "Hey, You are create a new Account!/n Best, MyTeam";

or this(if that has to be inside some HTML page)

String text = "Hey, You are create a new Account!<br/> Best, MyTeam";

or

String text = "Hey, You are create a new Account!" +System.getProperty("line.separator") + "Regards, MyTeam";
deadshot
  • 8,881
  • 4
  • 20
  • 39
javing
  • 12,307
  • 35
  • 138
  • 211
  • It's not work. I get "Hey, You are create a new Account!
    Best, MyTeam"
    – tomasz-mer Mar 30 '11 at 08:52
  • Show a bit more information about it. Where is that variable being called? Have you try with the line separator? – javing Mar 30 '11 at 08:55
  • @sfrj Using System.getProperty("line.separator") it doesn't seem to work but if you add \n directly it works fine. Did you tested the third approach as it doesn't seem to be working for me.Thanks – Gaurav Parek May 12 '20 at 15:43
  • Just figured that outlook was removing the extra line breaks. It seems to be working fine. – Gaurav Parek May 13 '20 at 11:45