Donnerstag, 17. Januar 2019

Java Mai API 1.6.2. and UTF8 headers com.sun.mail - javax.mail.jar OpenJDK 11

I went into some trouble when upgrading the System Concept DMS project to run with OpenJDK 11. Since Java 11 does not include an implementation of javax.mail anymore I downloaded the current implementation from maven repo:

At this time recent version is 1.6.2. Some mail related unit-tests failed. The reason was that mail headers added via MimeMessage.addHeader(String name, String data) now writes utf8 encoded strings into the mail source. I use MimeMessage.writeTo(OutputStream stream) to persist the message to an EML file. This file differed from the expected content (which was not uft-8 encoded) and so the tests failed.
So far so good. After fixing the tests by just replacing the expected results with utf-8 encoded data I ran into the next - and real - problem: the headers are written utf-8 encoded but they are not read properly when opening an EML file via new MimeMessage(Session session, InputStream in). I wasted some hours trying to find an outdated library somwhere in the sub-projects. I was not able to find any hints on the web.
Finally I found the solution in com.sun.mail sources: You will have to add a property "mail.mime.allowutf8" = "true" to the JavaMail Session:

Properties props = System.getProperties();
props.put("", "");
props.put("mail.transport.protocol", "smtp");
props.put("mail.mime.allowutf8", "true");
session = Session.getDefaultInstance(props, null);

When setting this property the com.sun.mail implementation reads utf-8 encoded headers. I do not know why this property is evaluated on reading but not on writing the headers. From my point of view this is inconsistent.

Keine Kommentare:

Kommentar veröffentlichen