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:
- getFolder(String name, boolean ignoreCase) - Gets the folder by name.
- getFolders() - Gets the folder hierarchy/collection of folders.
- getFolderHierarchy() - Gets the folder hierarchy.
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();
}
Getting Message Modified Date
The OlmMessageInfo.getModifiedDate property allows you to get the message modified date.
for (OlmMessageInfo messageInfo : inboxFolder.enumerateMessages()) {
Date modifiedDate = messageInfo.getModifiedDate();
}