kota srinu kota srinu - 5 months ago 25
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....
http://imgur.com/RMCOjyz

below is my code...

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


ajax call:

submitHandler:function(form){
var jsonObj = getData();
if(confirm("You are about to send Email Communication, Are you sure..?")){
$.ajax({
type: "POST",
url: $("#applicationUrl").val() +"/web/utilities/sendEmailMessage",
dataType: 'json',
cache:false,
headers: {
'Content-Type': 'application/json'
},
data:JSON.stringify(jsonObj),
});
}
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;
try{
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(emailFrom);
helper.setTo(emailAddrs);
helper.setSubject(emailSubject);
helper.setText(emailMsg, true);
mailSender.send(message);
return true;
}
catch(MessagingException mesx){
mesx.printStackTrace();
return false;
}
catch(MailException mx){
mx.printStackTrace();
return false;
}
catch(Exception ex){
ex.printStackTrace();
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..

output:

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..
Thanks

Answer

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");

Or

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