Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newline character is not interpreted correctly in MimeMessage?

StringBuffer emailMessage = new StringBuffer("Dear Scott");
emailMessage.append("\r\n");
emailMessage.append("Sending mail over internet");

So here is my formatted content when i inspect in debugger

Dear Scott,
Sending mail over internet

But when i receive it in thunderbird, i receive complete message in one line like below. Somehow newline character is not interpreted correctly when sending the content as html

 Dear Scott,Sending mail over internet

here is how i am sending the message as html

 MimeMessage msg = null;
 msg = new MimeMessage(mailSession);
 msg.setText(body, CHARSET_UTF_8, "html");

If i simply send the text as msg.setText(body, CHARSET_UTF_8) then i see the message in right format i.e "Sending mail over internet" in seen in next line. I am not getting why new line character is not interpreted correctly when sending the text as html?

like image 998
M Sach Avatar asked Nov 13 '12 09:11

M Sach


2 Answers

Because you're sending an HTML email, you have to use <br /> instead of \r\n, just like a classic HTML document (e.g. a .html file). When you use \r\n, the rendered content won't print a new line, but the source code of the mail will (and vice versa).

like image 137
sp00m Avatar answered Sep 19 '22 17:09

sp00m


HTML does not interpret newline characters. When you set the content type to "html", use "<br/>" instead of "\r\n":

StringBuffer emailMessage = new StringBuffer("Dear Scott");
emailMessage.append("<br/>");
emailMessage.append("Sending mail over internet");
like image 31
David Tanzer Avatar answered Sep 19 '22 17:09

David Tanzer