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

Aspose.Email for .NET provides an API for reading Outlook for Mac data files in OLM format. You can load an OLM file from the disk into an instance of the OlmStorage class and get the information about its contents, for example, folders, subfolders, and messages.

Read OLM file

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
string dst = dataDir + "OutlookforMac.olm";
using (OlmStorage storage = new OlmStorage(dst))
{
foreach (OlmFolder folder in storage.FolderHierarchy)
{
if (folder.HasMessages)
{
// extract messages from folder
foreach (MapiMessage msg in storage.EnumerateMessages(folder))
{
Console.WriteLine("Subject: " + msg.Subject);
}
}
// read sub-folders
if (folder.SubFolders.Count > 0)
{
foreach (OlmFolder sub_folder in folder.SubFolders)
{
Console.WriteLine("Subfolder: " + sub_folder.Name);
}
}
}
}

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.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
public static void Run()
{
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
OlmStorage storage = new OlmStorage(dataDir + "SampleOLM.olm");
PrintPath(storage, storage.FolderHierarchy);
}
public static void PrintPath(OlmStorage storage, List<OlmFolder> folders)
{
foreach (OlmFolder folder in folders)
{
// print the current folder path
Console.WriteLine(folder.Path);
if (folder.SubFolders.Count > 0)
{
PrintPath(storage, folder.SubFolders);
}
}
}

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.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
public static void Run()
{
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
OlmStorage storage = new OlmStorage(dataDir + "SampleOLM.olm");
PrintMessageCount(storage.FolderHierarchy);
}
public static void PrintMessageCount(List<OlmFolder> folders)
{
foreach (OlmFolder folder in folders)
{
Console.WriteLine("Message Count [" + folder.Name + "]: " + folder.MessageCount);
}
}