DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 85 posts at DZone. View Full User Profile

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

07.09.2014
| 4464 views |
  • submit to reddit
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();
                    }
              }
        }
  }
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. Aspose.Email for Android API enables developers to design Android applications for managing & manipulating Outlook email file formats without using MS Outlook. It provides tools to create, read & convert Outlook MSG, PST, EML, EMLX, OST & MHT file formats. It manages recipients, subject, message body, attachments in MSG documents.