Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NewLine Characters not working when formatting email with StringBuilder

I am doing simple formatting for an email with StringBuilder and have code that looks like the following.


StringBuilder message = new StringBuilder();
            message.append("Name: " + model.getName() + "\r\n");
            message.append("Organization: " + model.getOrganization() +"\r\n");
            message.append("Comment: " + model.getComment() +"\r\n");
            contactMessage.setMessage(message.toString());

I am logging the formatting and it works correctly, but it is coming out as one line when we actually check the emails being sent.

What if I am not using HTML though is my real question...thanks for the help.

like image 772
TheJediCowboy Avatar asked Jun 23 '10 18:06

TheJediCowboy


People also ask

How do I add a character to StringBuilder?

append(char c) method appends the string representation of the char argument to this sequence. The argument is appended to the contents of this sequence. The length of this sequence increases by 1.

How many characters can a StringBuilder hold?

Constructs a string builder with no characters in it and an initial capacity of 16 characters.

Does string format use StringBuilder?

To concatenate String we often use StringBuilder instead of String + String , but also we can do the same with String. format which returns the formatted string by given locale, format and arguments. In performance, is String.


1 Answers

What is the format of your email? If the format is HTML newline characters will be ignored and you'd need to insert HTML breaks <br />.

StringBuilder message = new StringBuilder();
message.append("Name: " + model.getName() + "<br />");
message.append("Organization: " + model.getOrganization() +"<br />");
message.append("Comment: " + model.getComment() +"<br />");
contactMessage.setMessage(message.toString());
like image 200
brainimus Avatar answered Sep 22 '22 10:09

brainimus