I need to send mails through java, where the Personal (sender name) of the InternetAddress can be arbitrary UTF8 strings. As my Email Address String may contain a personal or not, I’m solely using the InternetAddress( String ) constructor, to build an appropriate InternetAddress:
InternetAddress sender = new InternetAddress( "s\u00E8nder <from@example.org>" );
sender.setPersonal( sender.getPersonal(), Charsets.UTF_8.name() );
As from my understanding (as tests seem to support this) the basic InternetAddress Constructor does not perform any Personal encoding, so I’m doing this in an additional step, which encodes the Personal in conformance to MIME.
My Question is if this procedure may fail under some circumstances, javadoc says that .getPersonal() may return the raw data if decoding fails. But can this really ever happen?
And if it fails would that cause any troubles, as javadoc tells setPersonal does encoding only if it contains non ASCII, but the rawdata wont do that, right?
The constructor you’re using assumes the address string is already properly encoded, as it would be if you read it from an email header. You should maintain the email address and personal name fields separately, and use the constructor that takes them as separate arguments. If you don’t have a personal name, you can pass null.
You can get back a string of the format you’re using with the toUnicodeString method, but there’s no way to parse such a string.