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.
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.
Constructs a string builder with no characters in it and an initial capacity of 16 characters.
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With