Nondeterministic MD5 Message Digest for Email Attachment

I’m using Aspose Email for Java 21.6 and encountering an issue where an extracted attachment does not produce consistent contents. The attachment is an RFC822 message. It appears maybe there is a created date included in the saved attachment content which is causing the MD5 to be different. Is this expected behaviour? Is there a mechanism to ensure consistent MD5 values for extracted attachments from emails?

I’ve attached a code sample and a file to reproduce the issue.

aspose-email-attachment.zip (2.5 KB)

@tucker.barbour,
Thank you for the issue description. I will answer you as soon as possible.

@tucker.barbour,
I reproduced the problem and found no way to get the constant value of the MD5 message digest for that attachment. I logged the issue with ID EMAILJAVA-34925 in our tracking system. Our development team will investigate this case. I will inform you of any progress.

Thank you for the update.

@tucker.barbour,

Our development team investigated the issue. You are right. It is usual behavior for such a case. As a workaround, you can set creation time and the last modification time manually. Then the MD5 values will be consistent for extracted MapiMessage attachments:

try (OutputStream outputStream = Files.newOutputStream(attachmentName);
     DigestOutputStream digestOutputStream =
             new DigestOutputStream(outputStream, messageDigest)) {

    if (attachment.getObjectData() != null && attachment.getObjectData().isOutlookMessage())
    {
        MapiMessage attachmentMessage = attachment.getObjectData().toMapiMessage();
        // PidTagCreationTime
        attachmentMessage.setProperty(MapiProperty.createMapiPropertyFromDateTime(MapiPropertyTag.PR_CREATION_TIME, new Date(0)));
        // PidTagLastModificationTime
        attachmentMessage.setProperty(MapiProperty.createMapiPropertyFromDateTime(MapiPropertyTag.PR_LAST_MODIFICATION_TIME, new Date(0)));

        attachmentMessage.save(digestOutputStream);
    } else {
        attachment.save(digestOutputStream);
    }
}

Documents: Working with MAPI Properties
API Reference: MapiProperty class, MapiPropertyTag class

I can confirm this fixes the issue. Thank you.

The one small issue is that Date does not represent a time zone and it appears the timezone information is inferred from the JVM timezone set by the system or with “-Duser.timezone”. Is there a way to explicitly set the timezone as well? Or is using Date our only option?

@tucker.barbour,
Unfortunately, I did find no information you requested. I passed your query to our development team. I will answer you as soon as possible.

@tucker.barbour,
Outlook stores dates in MAPI messages in UTC format. When we specify the date for an MSG, the date is converted from the local timezone to UTC before being stored in the file. For example, a current time zone is +3. If you set the time 09:00 for the MSG, 06:00 (UTC) will be stored in the MSG file. If you open the MSG elsewhere with timezone -5, Outlook will display 01:00. We will be glad to receive any suggestions from you.

That makes sense. Thank you for the clarification.