1

I am sending a mail using java but when I define special characters in my message body they are not coming properly in the mail. The character I wrote in message body:

Hereâ €™s the checklist of what

and what I receive is:

Hereâ €™s the checklist of what

I'm getting the message body structure in jQuery using this method:

messageBody =  $('.messageBody').html();

and passing it to servlet using an AJAX call:

$.ajax({ 
    url: '/bin/mail',
    data: {    
        emailTo: mailTo, 
        emailFrom: mailFrom,
        attachmentPath: attachmentPath, 
        messageBody: messageBody, 
        emailSubject: emailSubject 
    },
    dataType: 'text', 
    type: 'POST',
    success: function(data){ 
        $('.thankyou').show();
    }, 
    error: function() { 
        alert("Error");
    } 
})

in my servlet

String messageBody = (String)request.getParameter("messageBody");

MimeMessage mimemessage = new MimeMessage(session);
mimemessage.setHeader("Content-Encoding","UTF-8");
mimemessage.setRecipients(javax.mail.Message.RecipientType.TO, recipients.toArray(new InternetAddress[recipients.size()]));
mimemessage.setFrom(new InternetAddress(from));
mimemessage.setReplyTo(new InternetAddress[] { new InternetAddress(recipient) });
mimemessage.setSubject(subject);
mimemessage.setSentDate(new Date());

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(messageBody, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

mimemessage.setContent(multipart);
moffeltje
  • 4,521
  • 4
  • 33
  • 57
user2142786
  • 1,484
  • 9
  • 41
  • 74

1 Answers1

0

It is hard to make a function to accept/replace all those special characters or symbols. But I think you can use encodeURIComponent(body) to encode the message body to accept at least some of the special characters.

Found this Link which shows an answer for a similar question.

Hope you can get an idea to solve it.

Community
  • 1
  • 1
Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43