I do this where addressString is an email address with NLS characters:
InternetAddress address = new InternetAddress(addressString); String personal = address.getPersonal(); if(personal != null) { address.setPersonal(personal, "utf-8"); }
getPersonal() gets the raw personal name, if any, because if you created InternetAddress with a single line or use InternetAddress.parse() , you wonβt have a partial name on a separate line:
public java.lang.String getPersonal ()
Get a personal name. If the name is encoded in accordance with RFC 2047, it is decoded and converted to Unicode. If decoding or conversion is not performed, the raw data is returned as is.
Then setPersonal() sets the string again, but this time tells InternetAddress to encode it:
public void setPersonal (name java.lang.String, java.lang.String charset)
Set a personal name. If the name contains non-US-ASCII characters, then the name will be encoded using the specified encoding in accordance with RFC 2047. If the name contains only US-ASCII characters, the encoding is not performed and the name is used as is.
Dseager
source share