Search code examples
javajakarta-mailimapgmail-apimime

InternetAddress Parse failing on Mime Headers because of certain emails


The InternetAddress.parse(String addressList, boolean strict) function part of the java-gmail-imapis used to parse email lists separated by , of the form host@domain and Personal Name <host@domain> that conform to the RFC822 standard.

This is typically used to parse the mime headers of an email (This is present in the MessagePartHeader in the Message object returned by the gmail api).

Question: The parse method throws an AddressException when it encounters an email address it cannot parse. The problem with this is that it is unable to parse the entire address list because of potentially one faulty/malformed/non-ascii email address. Is there a good way of splitting the mime headers (such as to, bcc, cc) into individual email addresses and parsing them individually? (This is not as simple as a split on , because names can potentially contain ,). This way I can localize the bad email address.

In addition, are there better java mime parsers that can parse emails that contain non-ascii characters in email addresses (RFC6530, RFC6531 and RFC6532)? This way I can have a better chance of parsing this localized faulty email address that doesn't conform to RFC822.


Solution

  • The latest version of JavaMail 1.6.0 has introduced a new session property, that allows for UTF-8 mail addresses. Just set mail.mime.allowutf8 to true in your Session properties.

    Properties props = new Properties();
    props.put("mail.mime.allowutf8", true);
    Session.getInstance(props)
    

    see the changelog