Opening OLM Files
Aspose.Email for .NET represents an Outlook for Mac storage file with the OlmStorage class. This article shows how to open an OLM file from a path or a stream, and how to safely load a file that may be corrupted.
Open an OLM File
An OLM file can be opened in two ways:
- using a constructor;
- using the static
FromFilemethod.
There is a difference in behavior between these two approaches that affects how the folder hierarchy is initialized. See Navigating OLM Folders for details.
Using the Constructor
To open a file, call the constructor of the OlmStorage class and pass the full file name as an argument. Because OlmStorage implements IDisposable, wrap it in a using statement so that the underlying file handle is released when you are done.
var fileName = "MyStorage.olm";
using (var olm = new OlmStorage(fileName))
{
// work with the storage
}
Using the Static FromFile Method
To open a file, use the static FromFile method and pass the full file name as an argument:
var fileName = "MyStorage.olm";
using (var olm = OlmStorage.FromFile(fileName))
{
// work with the storage
}
Open an OLM File from a Stream
When the OLM data does not come from a local path - for example, it is received from a database, a network resource, or cloud storage - you can open it directly from a Stream. Use the OlmStorage(Stream) constructor or the static FromStream method.
using (var stream = File.OpenRead("MyStorage.olm"))
using (var olm = new OlmStorage(stream))
{
// work with the storage
}
The static method works the same way:
using (var stream = File.OpenRead("MyStorage.olm"))
using (var olm = OlmStorage.FromStream(stream))
{
// work with the storage
}
Open a Corrupted OLM File
If a file may be partially corrupted, you can still extract as much data as possible without stopping on the first error. To do this, create the OlmStorage instance with the OlmStorage(TraversalExceptionsCallback callback) constructor, then load the file with the Load method instead of FromFile.
The callback receives the exceptions raised during loading and traversal, so you can log or ignore them. The Load method returns true if the file was loaded successfully and traversal is possible, and false if the file is too corrupted to traverse.
using (var olm = new OlmStorage((exception, id) =>
{
// Exception handling code.
Console.WriteLine($"Failed to process item '{id}': {exception.Message}");
}))
{
if (olm.Load(fileName))
{
// The file was loaded and can be traversed.
var folderHierarchy = olm.GetFolders();
}
else
{
Console.WriteLine("The OLM file is corrupted and cannot be traversed.");
}
}
For a complete example of extracting items from a corrupted file, see Reading and Extracting OLM Messages.