Search code examples
encodingjakarta-mail

javax.mail.Part and writeTo, unable to obtain the same "eml" file as the original one


My application parses many messages via javamail 1.5.6, it listens for incoming messages then store some info about them.

Almost all messages contain a digital signature, so my application needs to retrieve the full eml too, that is the raw file representing an email, in this way application users can always prove the validity of these messages.

So, once I have a javax.mail.Message, then I have to produces its eml, so I do:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
m.writeTo(baos);
this.originalMessage = baos.toString(StandardCharsets.UTF_8.name());

this approach generally works, but I had some multipart messages having part like the following:

This is a multi-part message in MIME format. --------------55D0DAEBFD4BF19F87D16E72 Content-Type: text/plain; charset=iso-8859-15; format=flowed Content-Transfer-Encoding: 8bit

In allegato si notifica ai sensi e per gli effetti dell'art. 11 R.D. 1611/1993, al messaggio PEC, oltre alla Relata di Notifica e contestuale attestazione di conformità, --------------55D0DAEBFD4BF19F87D16E72

word "conformità" is not properly transformed in the resulting string, it becomes "conformit�", opening such eml for example with MS Outlook results in an invalid digital signature, so message appears corrupted, different from the original

Have you same idea? Thank you very much


Solution

  • The raw message is not a UTF-8 encoded string, nor is an "eml" file a UTF-8 encoded file. They are both byte streams, and your digital signature should work on byte streams.

    In your particular example, the content of the message part is encoded using the iso-8859-15 charset, not UTF-8.