title: javax.mail error when parsing email address header with '[' character
body: <p>I'm trying to digest some messages coming in via a gmail account and one or more messages I receive have a formatted name in the header for the sender.  Something along the lines of </p>
  
  <p><code>[hris [hristerson &lt;chrisDoesNotExist@gmail.com&gt;</code></p>
  
  <p>The name above is fake, but illustrates the scenario I'm encountering, the name is replacing one or more 'C' characters with '['.</p>
  
  <p>The code that triggers the error </p>
  
  <pre><code>String header = "[hris [hristerson &lt;chrisDoesNotExist@gmail.com&gt;";
  boolean strict = true; // I also tried using a false value
  InternetAddress.parse(header, strict);
  </code></pre>
  
  <p>The error itself manifests as: <code>javax.mail.internet.AddressException: Missing ']'</code></p>
  
  <p>Some research I was doing was making me think that using encoded-words per RFC-2047 might be an answer.  But unless I was attempting it incorrectly, it didn't appear to work.  Additionally, other similarly illegal characters don't give me the same error per my testing, which makes me think that it's incidental, and is more uniquely a java library problem.</p>
  
  <p>The code that I used to attempt a rudimentary encoded-word fix was:</p>
  
  <pre><code>public String encodePrettyEmailHeader(String header, String charset, char encoding) {
  
      Map&lt;String, String&gt; illegalChars = new HashMap&lt;&gt;();
  
      illegalChars.put("\\[", "=5B");
      illegalChars.put("]", "=5D");
      illegalChars.put("\\(", "=28");
      illegalChars.put("\\);", "=29");
      illegalChars.put("&lt;", "=3C");
      illegalChars.put("&gt;", "=3E");
      illegalChars.put("@", "=40");
      illegalChars.put(",", "=2C");
      illegalChars.put(";", "=3B");
      illegalChars.put(":", "=3A");
      illegalChars.put("\"", "=22");
      illegalChars.put("/", "=2F");
      illegalChars.put("\\?", "=3F");
      illegalChars.put(" ", "=20");
  
      String email = getEmailFromHeader(header).trim(); // regex to get the &lt;email@address.com&gt; from the header
      String name = header.substring(0, header.indexOf(email)).trim();
  
      for (Map.Entry&lt;String, String&gt; anEntry : illegalChars.entrySet()) {
          name = name.replaceAll(anEntry.getKey(), anEntry.getValue());
      }
  
      return String.format("=?%s?%c?%s?= %s", charset, encoding, name, email);
  }    
  </code></pre>
  
  <p>Calling it like: <code>InternetAddress.parse(encodePrettyEmailHeader(header, "UTF-8", 'Q'), strict);</code></p>
  
  <p>I'm hoping that there is an answer that doesn't involve manually encoding the data into quoted-printable, I believe I should be able to make this janky solution work, however, this will add more overhead since I will need to un-encode it before the data can be usefully displayed.  </p>
  
  <p>Does anyone have any advice for me to proceed?</p>
  
  <p>Edit:</p>
  
  <p>I upgraded my version and the same result happened.  However, I realized that when I obfuscated the address header, I added an additional set of quotes beyond what arrives from gmail.  For brevity:</p>
  
  <p>This is how I incorrectly formatted it:
  <code>"[hris [hristerson" &lt;chrisDoesNotExist@gmail.com&gt;</code></p>
  
  <p>This is the correct format that fails for me:
  <code>[hris [hristerson &lt;chrisDoesNotExist@gmail.com&gt;</code></p>
  
  <p>Edit 2:</p>
  
  <p>For clarity, I'm using <code>group: 'com.sun.mail', name: 'javax.mail', version: '1.5.5'</code> and I've made the upgrade to <code>group: 'com.sun.mail', name: 'javax.mail', version: '1.6.0'</code> with no difference in behavior.</p>
  
  <p>I've also tried setting strict to false, but the exception thrown is no different.<a href="https://i.stack.imgur.com/4C3L6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4C3L6.png" alt="Debugger screenshot example"></a></p>