-2

I'm using Java 8 and Spring API to compose the mail body as below.

        StringBuilder emailTemplateForNewPassword = new StringBuilder();
        emailTemplateForNewPassword.append("Dear User,");

        emailTemplateForNewPassword.append(System.lineSeparator());
        emailTemplateForNewPassword.append(System.lineSeparator());

        emailTemplateForNewPassword.append("Sample Mail.");

        emailTemplateForNewPassword.append(System.lineSeparator());
        emailTemplateForNewPassword.append(System.lineSeparator());

        emailTemplateForNewPassword.append("Yours Faityfully,");
        emailTemplateForNewPassword.append(System.lineSeparator());
        emailTemplateForNewPassword.append("ABC.Com");
        emailTemplateForNewPassword.append(System.lineSeparator());
        emailTemplateForNewPassword.append("0094778999658");

Expected result:

Dear User,

Sample Mail.

Yours Faithfully,
ABC.Com
0094778999658

Actual Result:

Dear User,

Sample Mail.

Yours Faithfully, ABC.Com 0094778999658

I am not sure why the last 3 lines come on same line even that I used System.lineSeparator().

I have already tried "\r\n" but no luck.

Can anyone suggest what's wrong ?

I know that there are other questions related to this but none of them imsolve my problem. Please do not downcast this question until I find the answer.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Java-Seekar
  • 1,720
  • 5
  • 30
  • 52

1 Answers1

4

RFC 5322 describes the standard for email.

Per Section 2.1:

Messages are divided into lines of characters. A line is a series of characters that is delimited with the two characters carriage-return and line-feed; that is, the carriage return (CR) character (ASCII value 13) followed immediately by the line feed (LF) character (ASCII value 10). (The carriage return/line feed pair is usually written in this document as "CRLF".)

You need to use \r\n explicitly to separate your lines.

You can't rely upon System.lineSeparator(), because this is dependent upon JVM config; by default, that config will use just \n on Linux.

Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • emailTemplateForNewPassword.append("Yours Faityfully,"); emailTemplateForNewPassword.append("\r\n"); emailTemplateForNewPassword.append("ABC.Com"); emailTemplateForNewPassword.append("\r\n"); emailTemplateForNewPassword.append("0094778999658"); – Java-Seekar May 13 '19 at 10:49
  • Still I get the same issue – Java-Seekar May 13 '19 at 10:50
  • 1
    Some one downcast the question without solving the issue. Then what is the purpose of having forum like this? Really disappointed. I tried this whole day today but still no luck. – Java-Seekar May 13 '19 at 10:55
  • 1
    @Java-Seekar actually, two people have (neither of whom are me). I'd imagine because you haven't provided enough information to actually reproduce the issue. – Andy Turner May 13 '19 at 11:00
  • 1
    I have fixed this issue by using MimeMessage and set the content type as "text/html". I have separated the lines using
    . The person who down voted this question couldn't provide the correct answer. Please don't de motivate us by down voting.
    – Java-Seekar May 17 '19 at 03:33