电子邮件主题中的特殊符号

时间:2011-05-03 15:11:21

标签: javamail html-email

我们目前正在制作一个缺陷,允许在电子邮件主题中看到特殊符号。电子邮件是text / html mime类型。

目前,如果受试者应具有心脏符号,则其显示为“& heart”,但在电子邮件正文中显示“心脏”符号。

有人可以帮助我们解决方案,让特殊符号也成为主题的一部分吗?

以下是代码段。

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)抛出异常{

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;

}

2 个答案:

答案 0 :(得分:2)

  public static String HTMLDecode(String encodedHTML) {

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

答案 1 :(得分:1)

您可以发送为Unicode / UTF-8。