JavaMail SEARCH returns BADCHARSET

Viewed 65

I have developed an email client at work using JavaMail. Some customers have reported massive delays when searching for subjects with non-English characters (e.g. "Kühlgut").

It turned out that some mail providers (in this case Office 365) do not support searching with UTF-8 charset (resulting in a BadCommandException). Therefore, JavaMail fetches and performs the search locally (as fallback).

This behaviour caused the massive delays.

Code:

String searchTerm = "Kühlgut";
SearchTerm[] searchTerms = new SearchTerm[3];
searchTerms[0] = new FromStringTerm(searchTerm);
searchTerms[1] = new SubjectTerm(searchTerm);
searchTerms[2] = new BodyTerm(searchTerm);

OrTerm orTerm = new OrTerm(searchTerms);
folder.search(orTerm);

JavaMail debug output of the search command:

C18 SEARCH CHARSET UTF-8 OR OR FROM {8+}
Kühlgut SUBJECT {8+}
Kühlgut BODY {8+}
Kühlgut ALL
C18 NO [BADCHARSET (US-ASCII)] The specified charset is not supported.
C19 SEARCH CHARSET Cp1252 OR OR FROM {7+}
Kühlgut SUBJECT {7+}
Kühlgut BODY {7+}
Kühlgut ALL
C19 NO [BADCHARSET (US-ASCII)] The specified charset is not supported.

B20 FETCH 1 (ENVELOPE INTERNALDATE RFC822.SIZE)
* 1 FETCH (ENVELOPE ....)
B20 OK FETCH completed.
B21 FETCH 1 (BODYSTRUCTURE)
* 1 FETCH (BODYSTRUCTURE ...
B21 OK FETCH completed.

I do have two questions:

  1. Is it possible to check if the IMAP server supports searching with UTF-8 charset (e.g. via capabilities)?
  2. How can I stop JavaMail from fetching all emails if the former is not possible (no exception is thrown)?
1 Answers

I don't think there's a way to do #1, but for #2 try setting the mail.imap.throwsearchexception property to true.

And submit a bug to Microsoft since Office 365 really should support searching with utf-8.

Related