Read Outlook for Mac OLM File and Get Folders and SubFolders Information

Open OLM format files

OLM format files can be opened in two ways:

  • using constructor
  • using static FromFile method

There are differences in behavior between these methods. See section below.

Open files by constructor

To open a file, call constructor of the OlmStorage class and pass full file name or stream as an argument to it:

OlmStorage olm = new OlmStorage("fileName");

Open files using static method FromFile

To open a file, use static method fromFile and pass full file name as an argument to it:

OlmStorage olm = OlmStorage.fromFile("fileName");

Open files using static method FromStream

To open a file using static method fromStream, pass a stream name as an argument to it:

Read OLM file

The following code snippet shows you how to load the OLM file and get its content.

Get folders

To retrieve folders from an OLM (Outlook for Mac) file, load it with the OlmStorage class first, and then use one of the following methods depending on your needs:

The code sample below will show you how to get a folder from an OLM file:

// Get the folder by name
OlmStorage olm = OlmStorage.fromFile("fileName");

try {

    OlmFolder inbox = olm.getFolder("inbox", true);

} finally {

    olm.dispose();

}

When using the fromFile method to open an OLM file, the getFolderHierarchy() will return null. In this case, call the getFolders() method explicitly to retrieve the list of directories in the OLM file.

Get Folder Path

You may also get the folder path of folders in the OML file. Aspose.Email provides OlmFolder.Path property which returns the folder path. The following code snippet demonstrates the use of OlmFolder.Path property to get the folder paths in the OML file.

Count the number of items in the folder

You may also count the number of items in the folder. Aspose.Email provides OlmFolder.MessageCount property which returns the number of items in the folder. The following code snippet demonstrates the use of OlmFolder.MessageCount property to get the number of items in the folders of the OML file.

Enumerate Messages from a given Folder

Aspose.Email provides the API to work with OLM storages and extract messages from a given folder. The OlmFolder and OlmMessageInfo classes represent brief information about a folder and a message in the storage respectively. The OlmFolder class provides the following methods:

  • OlmFolder.getSubFolder(String subfolderName, boolean ignoreCase) - Gets the subfolder by name.

  • OlmFolder.enumerateMapiMessages() - Exposes the enumerator, which supports an iteration of MapiMessages in the current folder.

  • OlmFolder.enumerateMessages() - Exposes the enumerator, which supports an iteration of OlmMessageInfo’s in the current folder.

  • OlmFolder.enumerateMessages(int startIndex, int count) - Exposes the enumerator, which supports an iteration of OlmMessageInfo’s within a given range.

  • OlmFolder.enumerateMessages(MailQuery query) - Exposes the enumerator, which supports an iteration of OlmMessageInfo’s by search criteria.

The code samples below will demonstrate the use of these methods:

 // Enumerates all messages in a given folder

OlmStorage olm = OlmStorage.fromFile("fileName");

try {

    OlmFolder inbox = olm.getFolder("inbox", true);

   for (OlmMessageInfo messageInfo : inbox.enumerateMessages()) {

        System.out.println(messageInfo.getSubject());

   }

} finally {

    olm.dispose();

}

// Enumerates a range of messages in a given folder

OlmStorage olm = OlmStorage.fromFile("fileName");

try {

    OlmFolder inbox = olm.getFolder("inbox", true);

   int startIndex = 10;

   int count = 100;

   for (OlmMessageInfo messageInfo : inbox.enumerateMessages(startIndex, count)) {

        System.out.println(messageInfo.getSubject());

   }

} finally {

    olm.dispose();

}

// Enumerates messages by search criteria

OlmStorage olm = OlmStorage.fromFile("fileName");

try {

    OlmFolder inbox = olm.getFolder("inbox", true);

    MailQueryBuilder mailQueryBuilder = new MailQueryBuilder();

    mailQueryBuilder.getSubject().contains("invitation");

    mailQueryBuilder.getFrom().contains("Mark");

   for (OlmMessageInfo messageInfo : inbox.enumerateMessages(mailQueryBuilder.getQuery())) {

        System.out.println(messageInfo.getSubject());

   }

} finally {

    olm.dispose();

}

// Enumerates all messages and the extraction of some of them

OlmStorage olm = OlmStorage.fromFile("fileName");

try {

    OlmFolder inbox = olm.getFolder("inbox", true);

   for (OlmMessageInfo messageInfo : inbox.enumerateMessages()) {

       if (messageInfo.hasAttachments()) {

            MapiMessage msg = olm.extractMapiMessage(messageInfo);

       }

   }

} finally {

    olm.dispose();

}

Extract Messages from OLM by Identifiers

Sometimes it is required to extract selected messages by identifiers. For example, your application stores identifiers in a database and extracts a message on demand. This is the efficient way to avoid traversing through the entire storage each time to find a specific message to extract. To implement this feature for OLM files, Aspose.Email provides the following methods and classes:

The code sample below demonstrates how to extract messages from OLM by identifiers:

for (OlmMessageInfo msgInfo : olmFolder.enumerateMessages()) {
    MapiMessage msg = storage.extractMapiMessage(msgInfo.getEntryId());
}

Note: The message ID is unique within the storage file. IDs are created by Aspose.Email and cannot be used in other third-party OLM processing libs or apps.

Extract OLM Items from Corrupted Files

Aspose.Email provides a traversal API which allows extracting all OLM items as far as possible, without throwing out exceptions, even if some data of the original file is corrupted.

Use the OlmStorage(TraversalExceptionsCallback callback) constructor and the load(String fileName) method instead of the fromFile method.

The constructor allows defining a callback method.

OlmStorage olm = new OlmStorage(new TraversalExceptionsCallback() {
    public void invoke(TraversalAsposeException exception, String itemId) {
        /* Exception handling  code. */
    }
});

Loading and traversal exceptions will be available through the callback method.

The load method returns ‘true’ if the file has been loaded successfully and further traversal is possible. If a file is corrupted and no traversal is possible, ‘false’ is returned.

TraversalExceptionsCallback exceptionsCallback = new TraversalExceptionsCallback() {
    public void invoke(TraversalAsposeException exception, String itemId) {
        /* Exception handling  code. */
    }
};
try (OlmStorage olm = new OlmStorage(exceptionsCallback)) {
    if (olm.load(fileName)) {
        List<OlmFolder> folderHierarchy = olm.getFolders();
        extractItems(olm, folderHierarchy);
    }
}

private static void extractItems(OlmStorage olm, List<OlmFolder> folders) {
    for (OlmFolder folder : folders) {
        if (folder.hasMessages()) {
            System.out.println(folder);

            for (MapiMessage msg : olm.enumerateMessages(folder)) {
                System.out.println(msg.getSubject());
            }
        }

        if (folder.getSubFolders().size() > 0) {
            extractItems(olm, folder.getSubFolders());
        }
    }
}

Get Message Modified Date

The OlmMessageInfo.getModifiedDate property allows you to get the message modified date.

for (OlmMessageInfo messageInfo : inboxFolder.enumerateMessages()) {
    Date modifiedDate = messageInfo.getModifiedDate();
}