0

I am sending emails with the Spring MimeMessageHelper, which uses the JavaMail API in the background. I would like to have an option to input the recipient's name like this:

private static void setTo(final EmailDTO emailDTO, MimeMessageHelper helper) throws MessagingException {
  if (StringUtils.isBlank(emailDTO.getToName())) {
    helper.setTo(emailDTO.getToEmail());
  } else {
    helper.setTo(emailDTO.getToName() + " <" + emailDTO.getToEmail() + ">");
  }

}

However, this throws a

javax.mail.internet.AddressException: Illegal character in address ( Őry József <mail@testcompany.com> )

..when there are accented characters in the name.Which function shall I use for encoding the name in this case?

Peter
  • 715
  • 1
  • 12
  • 23

1 Answers1

2
MimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("stalin@ussr.su", "Товарищ Сталин"));

Results in:

To: =?UTF-8?B?0KLQvtCy0LDRgNC40Ykg0KHRgtCw0LvQuNC9?= <stalin@ussr.su>

See http://docs.oracle.com/javaee/6/api/javax/mail/Message.html#addRecipient(javax.mail.Message.RecipientType, javax.mail.Address)

pingw33n
  • 12,292
  • 2
  • 37
  • 38
  • 1
    Спасибо! :-) For those using Spring, the MimeMessageHelper.setTo() method also accepts an InternetAddress instance. – Peter May 30 '14 at 12:13