MapiAttachment is interpreting EML attachments as MSG attachments

Aspose Team,

Using your Java Email APIs version 21.2, we’re trying to process the attachments that are included as part of parent MSG documents as MapiAttachment items. What we’re seeing is that attachments that are EMLs are being identified as MSG attachments (sample code snippet below with example files attached).
Aspose_MSG_with_EML_Attachments_Examples.zip (434.0 KB)

The first example file, “Attachments with attachments.msg”, has an attachment that doesn’t show a file extension when viewed in Outlook so this may be more understandable. However, the second example file, “SMC.PC.00000007.msg”, has an attachment that has an “EML” file extension but is still identified as a “MSG” attachment. This appears to be a bug but maybe there’s an explanation on why/how we can identify these attachments as “EML” instead of “MSG” attachments. Looking forward to hearing more about this. Thanks for your help.

String filePath = "Attachments with attachments.msg";
handleAttachments(filePath);
filePath = "SMC.PC.00000007.msg";
handleAttachments(filePath);

public static void handleAttachments(String filePath) throws Exception {
	MapiMessage msg = MapiMessage.fromFile(filePath);
	for (int i = 0; i < msg.getAttachments().size(); i++) {
		MapiAttachment attachment = (MapiAttachment) msg.getAttachments().get_Item(i);
		if(attachment != null) {
			String fileName = attachment.getFileName();
			String extension = attachment.getExtension();
			Logger.Debug("Attachment fileName=" + fileName + ", extension=" + extension);
		}
	}
}

@jmuth,
Thank you for the issue description. I reproduced the problems and logged the issue in our tracking system with ID EMAILJAVA-34889. Our development team will investigate these cases. You will be notified when it is fixed.

Thanks, Andrey. I appreciate your quick response. We’ll look forward to hearing when there’s a fix.

@jmuth,
Our development team investigated the issue. Attachment object data in these files is data in Outlook MSG format. Aspose.Email identified the attachment data as Outlook MSG format data and changed the extension to “MSG”. Probably the value of PR_ATTACH_EXTENSION/PR_ATTACH_MIME_TAG properties in these files was specified incorrectly. Could you please clarify, how was the “SMC.PC.00000007.msg” file created?

Hello @Andrey_Potapov,
This file was created by attaching a .eml message to another email message and then sending it. The .eml was created from the mail app on a Macbook, but I am sure any mail client capable of creating .eml would also work.

@aweech,
Thank you for the additional information. I passed it to our development team.

@aweech,
The “SMC.PC.00000007.msg” file attachment is an object containing MSG format data. If you want the file extension to match the content, you can convert it to EML using our API.

How to detect file format:

MapiAttachment attachment = msg.getAttachments().get_Item(i);
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
    attachment.save(bos);
    try (ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray())) {
        MailMessage embedEml;
        // how to detect file format
        FileFormatInfo fileFormat = FileFormatUtil.detectFileFormat(bis);
        bis.reset();
        if (fileFormat.getFileFormatType() == FileFormatType.Msg) {
            // load and convert MSG to EML
            embedEml = MapiMessage.load(bis, new MsgLoadOptions()).toMailMessage(new MailConversionOptions());
        } else {
            embedEml = MailMessage.load(bis);
        }
        System.out.println(embedEml.getSubject());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Automatic conversion:

MapiAttachment attachment = msg.getAttachments().get_Item(i);
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
    attachment.save(bos);
    try (ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray())) {
        // if the stream is MSG format, it is automatically converted to MailMessage
        MailMessage embedEml = MailMessage.load(bis);
        System.out.println(embedEml.getSubject());
    }
} catch (IOException e) {
    e.printStackTrace();
}

Documents: Working with Attachments and Embedded Objects
API Reference: MapiAttachment class, FileFormatUtil class