Attached file extension get lost

We use Aspose Total Java, now we have such a problem:

There is such a test file, if you open it with Outlook, you will find that this Email contains an attached file. If you use Outlook to save this attachment, the file extension will be “.msg”

However, if we use Aspose Email to download this attachment, there is no file extension at all.

Test-Email.zip (327,9 KB)

Hello @zwei,

Could you check the LongFileName and Extension properties of the attachment:

MapiMessage msg = MapiMessage.load("Test-Email9.msg");
System.out.println(msg.getAttachments().get_Item(0).getObjectData().isOutlookMessage());
System.out.println(msg.getAttachments().get_Item(0).getLongFileName());
System.out.println(msg.getAttachments().get_Item(0).getExtension());

Output:

true
WG.msg
.msg

Here it is our code, and it has the problem I mentioned here


        final MapiMessage msg = MapiMessage.fromMailMessage(emailFilename);

        Files.createDirectories(Paths.get(tempDir));

        final ArrayList<String> result = new ArrayList<>(1);

        for (int i = 0; i < msg.getAttachments().size(); i++)
        {
            //Set a reference to the MapiAttachment object
            final MapiAttachment outlookMessageAttachment = (MapiAttachment) msg.getAttachments().get_Item(i);

            if(downloadInlineAttachmentAllowed || !outlookMessageAttachment.isInline())
            {
                //Display attachment type
                Log.info(AsposeHelper.class, "Att Type : " + outlookMessageAttachment.getMimeTag());
                Log.info(AsposeHelper.class, "File name : " + outlookMessageAttachment.getLongFileName());

                String attachmentName = outlookMessageAttachment.getDisplayName();
                //... ...

            }
        }

I have also tried your code, and it works, however the result of getMimeTag() of your code is null.

So the real question is, what is the difference between MapiMessage.fromMailMessage(String filename) and MapiMessage.load(filename)?

@zwei,

You have shared a file in Outlook MSG format (Test-Email 9.msg). The “MapiMessage.fromMailMessage” method in your case performs a double conversion. Outlook MSG will be converted to MIME format and then back to Outlook MSG:

final MapiMessage msg = MapiMessage.fromMailMessage(emailFilename);

Instead, use the “MapiMessage.load” method to load the Outlook MSG format message:

final MapiMessage msg = MapiMessage.load(emailFilename);

The original message does not have an attachment property “PR_ATTACH_MIME_TAG”(getMimeTag() is null).
You can determine the attachment type as an embedded message in a loop:

//Display attachment type
if (outlookMessageAttachment.getObjectData() != null && outlookMessageAttachment.getObjectData().isOutlookMessage())
    System.out.println("Att Type : Outlook Message");
else
    System.out.println("Att Type : " + outlookMessageAttachment.getMimeTag());
1 Like

Thanks for your explanation, however I have still such a question:

In our original code, e.g. using “fromMailMessage” Methode, the MimeTag returns “message/rfc822”

How does this “message/rfc822” come?

@zwei,

This behavior is caused by double conversion:

1.Outlook MSG will be converted to MIME format, all MAPI properties will be lost.
Embedded Attachment will be converted from Outlook MSG format to MIME format and added with Content-Type: message/rfc822
2.Mime message will be converted to Outlook MSG
A property PR_ATTACH_MIME_TAG = message/rfc822 will be created for the embedded attachment

1 Like

Thank you very much! So if it is Outlook MSG file, we should use “load” methode, but when should we use the “fromMailMessage” methode?

@zwei,

If the original message is in MIME format (*.eml), the “fromMailMessage” method must be used:

final MapiMessage msg = MapiMessage.fromMailMessage("message.EML");
1 Like

Thank you very much for your detailed explanation! Спасибо!

@zwei ,

Thank you for using our product.