Gen
Gen

Reputation: 2540

String formatting is not working in mail sending

My string is like below

String content  = 
"Dear user,

    <font face="Arial">Hello</font>

Regards,
Gen";

I am passing this content to the mail sender.

    `MimeMessage message = emailSender.createMimeMessage();
    String encodingOptions = "text/html; charset=UTF-8; format=flowed";
    try{
        message.setHeader("Content-Type", encodingOptions);
        message.setSentDate(new Date());
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
        boolean isHTML = true;
        helper.setFrom(mailFrom);
        helper.setTo(to.replaceAll("\\s",""));
        helper.setSubject(subject);
        helper.setText(content, isHTML);`
        

And this content String I am passing to the mail send like above, and it is in the format of html. When I recieve the mail I am getting the mail like below

Dear user,Hello Regards,Gen

How can I display the content in the below format.

Dear user,

    Hello

Regards,
Gen

Upvotes: 2

Views: 299

Answers (2)

Michał Schielmann
Michał Schielmann

Reputation: 1382

The message is exactly what you've created. In short HTML doesn't care about spaces and newlines. You can test your HTML online to see how it looks, ie. here: https://htmledit.squarefree.com/

For your message to be properly formatted, try something between the lines of:

<html>
<span>Dear user,</span></br></br>
<span><font face="Arial">Hello</font><span></br></br>

<span>Regards,</span></br>
<span>Gen</span>
</html>

You need to provide breaklines yourself. You could probably omit <span> tags.

Upvotes: 3

akuma8
akuma8

Reputation: 4691

Since it's HTML you have to add the line breaks yourself. I am not a frontend developer but you can use <p> tag or <br/>. Try something like:

String content  = 
"Dear user, <br/>

   <p> <font face="Arial">Hello</font></p>

Regards, <br/>
Gen";

Upvotes: 3

Related Questions