Sandeep
Sandeep

Reputation: 586

Special symbols in subject of emails

We are currently working on a defect to allow special symbols also to be seen in email subject. The email is text/html mime type.

Currently, if the subject should have a heart symbol it is shown as "&heart" but in the email body a "heart" symbol is shown.

Can someone help us with the solution to have special symbols also part of subject?

Here is the code snippet.

public boolean send(String to, String from, String subject, String templatePath, Map map) {
// create a mime message using the mail sender implementation
MimeMessage mimeMessage = mailSender.createMimeMessage();

// create the message using the specified template
MimeMessageHelper helper;
try
{

  helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
  helper.setTo(to);
  helper.setSubject(subject);
  helper.setFrom(from);

  String text = VelocityEngineUtils.mergeTemplateIntoString(engine, templatePath, map);
  helper.setText(text, true);
  send(mimeMessage);
  log.debug("in send  at start" + this.getClass().getName()
            + ":SUCCESS: Sendig mail to" + to + " from " + from + " subject "
            + subject);
} catch (MessagingException e)
{
  throw new MailPreparationException("unable to create the mime message helper", e);
} catch (Exception e)
{
  log.debug("in send  at start" + this.getClass().getName() + ":Failed sending mail"
            + to + " from " + from + " subject " + subject);
  // throw new
  // MailPreparationException("unable to create the mime message helper",
  // e);
}
return false;

} public boolean send(MimeMessage mimeMessage) throws Exception {

try
{

  Multipart multipart = new MimeMultipart();
  BodyPart bodyPart = new MimeBodyPart();
  multipart.addBodyPart(bodyPart);
  bodyPart.setContent(mimeMessage.getContent(), "text/html");
  mimeMessage.setContent(multipart);
  mailSender.send(mimeMessage);
} catch (Exception e)
{
  log.error("in send  at start" + this.getClass().getName() + ":Failed sending mail"
            + e.getMessage());

  // e.printStackTrace();
  throw e;
  // return false;
}
return true;

}

Upvotes: 1

Views: 1489

Answers (2)

Sandeep
Sandeep

Reputation: 586

  public static String HTMLDecode(String encodedHTML) {

        return encodedHTML.replaceAll("¡", "\u00A1")
                          .replaceAll("¢", "\u00A2")
                          .replaceAll("£", "\u00A3")
                          .replaceAll("¤", "\u00A4")
                          .replaceAll("¥", "\u00A5")
                          .replaceAll("¦", "\u00A6")
                          .replaceAll("§", "\u00A7")
                          .replaceAll("¨", "\u00A8")
                           ........

Upvotes: 2

Dan Blows
Dan Blows

Reputation: 21184

You can send as Unicode / UTF-8.

Upvotes: 1

Related Questions