Identify & Extract Embedded Attachment from MSG Formatted as RTF in Android - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Identify & Extract Embedded Attachment from MSG Formatted as RTF in Android

sheraz786sheraz786 Posts: 19Member

This technical tip explains how to identify and extract embedded attachment from MSG formatted as RTF. Email messages with an RTF formatted body may contain inline attachments that are either embedded as a whole object or as an icon. In order to differentiate between these two types of attachments, certain properties of the attachment need to be investigated. After meeting certain criteria based on the attachment properties, the attachment can be saved by extracting it from its ObjectData.

This tutorial identifies and extracts embedded attachment from MSG file formatted as RTF.

static void ExtractInlineAttachments() { MapiMessage message = MapiMessage.fromFile("Test.msg"); MapiAttachmentCollection attachments = message.getAttachments(); for (Object untypedAttachment : attachments) { MapiAttachment attachment = (MapiAttachment) untypedAttachment; if(IsAttachmentInline(attachment)) { try { SaveAttachment(attachment, UUID.randomUUID().toString()); } catch (IOException | FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } static boolean IsAttachmentInline(MapiAttachment attachment) { MapiObjectProperty objectData = attachment.getObjectData(); if (objectData == null) return false;

    for (Object prop : attachment.getObjectData().getProperties().getValues())
    {
          MapiProperty property = (MapiProperty)prop;
          if ("\u0003ObjInfo".equals(property.getName()))
          {
                byte[] data = property.getData();
                int odtPersist1 = data[1] << 8 | data[0];
                return (odtPersist1 & 0x40) == 0;
          }
    }
    return false;

} static void SaveAttachment(MapiAttachment attachment, String fileName) throws IOException, FileNotFoundException { for (Object prop : attachment.getObjectData().getProperties().getValues()) { MapiProperty property = (MapiProperty)prop; if ("Package".equals(property.getName())) { FileOutputStream fs; try { fs = new FileOutputStream(fileName); fs.write(property.getData(), 0, property.getData().length); } catch (java.io.IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

Tagged:
Sign In or Register to comment.