kota srinu kota srinu - 1 year ago 110
jQuery Question

Formatting email text from jsp in HTML format using spring

i am implementing email service using spring, where users can enter the email subject and the Email body from the form and can send an email. Here i was able to send the email successfully..but I need to translate the line breaks/tabs entered by the user in the email body form and need to translate to the same in actual email that will be sent...as right now it don't translate the linebreaks and send all the text entered by the user in a single line..Need help in formatting this..

img link is one of the usecase with linebreaks in my email....

below is my code...

<div class="email-sm-textDiv">
<textarea name="emailBodyTxtArea" id="emailBodyTxtArea"
placeholder="Email Body"></textarea>

ajax call:

var jsonObj = getData();
if(confirm("You are about to send Email Communication, Are you sure..?")){
type: "POST",
url: $("#applicationUrl").val() +"/web/utilities/sendEmailMessage",
dataType: 'json',
headers: {
'Content-Type': 'application/json'
return false;
function getData(){
var object ={
emailSubject : $("#emailSubTxtArea").val(),
emailMsg : $("#emailBodyTxtArea").val()

return object;


spring code:

private boolean executeSendingEmail(String[] emailAddrs, String emailSubject, String emailMsg) throws MessagingException,MailException, Exception {
MimeMessage message = null;
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setText(emailMsg, true);
return true;
catch(MessagingException mesx){
return false;
catch(MailException mx){
return false;
catch(Exception ex){
return false;


I was able to send email successfully..But it is not in the format entered by the user in the form..below is my o/p email..


HI Team, this is Test mail sending form Admin Application.... Sending this email for testing purpose... Ignore this mail.. regards, Admin.

So need help in formatting the email Msg entered by the user - which should be the same as entered by user..

Answer Source

Text entered from a <textarea> will have line breaks (\n) and HTML will interpret them as whitespace. Hence on the controller side, you need to do this:

String message = input.replace("\n", "<br />\n");


message = input.replace("\n", "<br>");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download