JavaMail: "Domain contains control or whitespace in string" errormessage because of domain with Danish characters
Currently mail servers generally don't accept non-ASCII characters in the local part, only the domain part (following the '@' sign) is supported with IDN.
To encode only the domain part with the java.net.IDN class, i use the following Util.
(Code not tested in production, but it should work)
import java.net.IDN;
public class IDNMailHelper {
public static String toIdnAddress(String mail) {
if (mail == null) {
return null;
}
int idx = mail.indexOf('@');
if (idx < 0) {
return mail;
}
return localPart(mail, idx) + "@" + IDN.toASCII(domain(mail, idx));
}
private static String localPart(String mail, int idx) {
return mail.substring(0, idx);
}
private static String domain(String mail, int idx) {
return mail.substring(idx + 1);
}
}
Java Mail doesn't support i18n domain names, so you must use the standard rules to escape them using the IDNA rules.