My code logs the message ID and other headers of incoming mail messages. I recently changed the IMAP server from which I'm reading incoming messages. When I did so, about 20% of the messages had "null" for the message ID.
I discovered that some messages had a "Message-ID" header, while the others had a "Message-Id" header (the only difference being the case of the last letter in the header name). So my code now looks like this:
String id = null;
String[] idHeaders = message.getHeader("Message-ID");
if (idHeaders != null && idHeaders.length > 0)
id = idHeaders[0];
else
{
System.out.println("\"Message-ID\" header not found during message conversion; trying \"Message-Id\".");
idHeaders = message.getHeader("Message-Id");
if (idHeaders != null && idHeaders.length > 0)
id = idHeaders[0];
else
{
System.out.println("No message ID headers found during message conversion; generating an artificial one.");
IObjectRetriever<String> idRetriever = getIDRetriever();
id = idRetriever.retrieveObject();
}
}
As you can see, I have to check for the existence of the "Message-ID" first. If it doesn't exist, then I check for the "Message-Id" header.
I examined what I think is the javamail source code at https://java.net/projects/javamail/sources/mercurial/content/mail/src/main/java/javax/mail/internet/InternetHeaders.java, and it appears that the matching on header names is case insensitive. So while I've found a workaround for my problem, I don't understand why it's necessary.