Recursive AI

Recover a damaged Outlook .msg file in C#

OneMail is a free, open-source email library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Use MsgFile.TryOpen with recovery enabled to read what survives without changing the file.

Terminal
dotnet add package RecursiveAI.OneMail

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported. Use damaged.msg from the download.

C#
using RecursiveAI.OneMail.Msg;
 
var options = new MsgReaderOptions
{
    Recovery = true,
    MaxRecoveryBytes = 4 * 1024 * 1024
};
if (!MsgFile.TryOpen("damaged.msg", out var file,
                     out var defects, options))
{
    foreach (var defect in defects)
        Console.Error.WriteLine(defect.Kind);
    return 1;
}
using (file)
{
    Console.WriteLine(
        $"Recovered: {file.IsRecovered}");
    Console.WriteLine(
        $"Subject: {file.Message.Subject}");
    foreach (var defect in file.ReadEverything())
        Console.WriteLine(defect.Kind);
}
return 0;

Unzip the example, then dotnet run.

The snippet prints the recovered subject and the types of findings:

Text
Recovered: True
Subject: Meeting notes
CompoundFileRecovered
CompoundFileDamage

A compound file is the container format that MSG files use.

Full program

The sample is a synthetic MSG with its final byte cut off. Pass a path to inspect another file: dotnet run -- path/to/message.msg. The full program also prints the recovery details and file offsets for container damage.

C#
using RecursiveAI.OneMail.Mapi;
using RecursiveAI.OneMail.Msg;
 
string path = args.FirstOrDefault() ?? "damaged.msg";
var options = new MsgReaderOptions
{
    Recovery = true,
    MaxRecoveryBytes = 4 * 1024 * 1024
};
if (!MsgFile.TryOpen(path, out var file,
                     out var defects, options))
{
    foreach (var defect in defects)
        Console.Error.WriteLine($"{defect.Kind}: {defect.Detail}");
    return 1;
}
using (file)
{
    Console.WriteLine($"Recovered: {file.IsRecovered}");
    Console.WriteLine($"Subject: {file.Message.Subject}");
    foreach (var defect in file.ReadEverything())
    {
        if (defect.Kind == MapiDefectKind.CompoundFileDamage)
            Console.WriteLine($"{defect.Kind} at byte {defect.Offset}: {defect.Detail}");
        else
            Console.WriteLine($"{defect.Kind}: {defect.Detail}");
    }
}
return 0;

dotnet run prints:

Text
Recovered: True
Subject: Meeting notes
CompoundFileRecovered: the compound file is damaged; it was opened by recovery, which found 1 problem(s). Properties and streams may be missing or cut short
CompoundFileDamage at byte 3072: Truncated, 511 byte(s) at file offset 3072: a whole FAT sector (the file ends inside it); the missing entries are free

If the header, allocation table, or directory can’t be read or recovered, TryOpen returns false with defects instead of a message. A missing or inaccessible path can still throw an I/O exception.

Variations

Try a strict read first. On the same damaged sample, this reports CompoundFileInvalid and prints no message:

C#
using RecursiveAI.OneMail.Msg;
 
if (MsgFile.TryOpen("damaged.msg", out var file, out var defects))
{
    using (file)
        Console.WriteLine(file.Message.Subject);
}
else
{
    foreach (var defect in defects)
        Console.WriteLine(defect.Kind);
}

Read an Outlook .msg file · Write an Outlook .msg file from a MAPI message


The short answer

Use MsgFile.TryOpen with Recovery = true to read what remains of a damaged .msg file. Set MaxRecoveryBytes to bound the scan for lost directory sectors. On success, check IsRecovered, read the fields you need, and log the findings. On failure, report the defects returned through the out parameter. This reads the file; it does not write a repaired copy.

Can you recover a corrupted Outlook .msg file?

Sometimes. An MSG is a single Outlook item in a compound file. Its directory and allocation tables tell the reader which parts of the file belong to each stream. The recovery reader can work around some broken sector chains, bad directory links, and missing tail bytes. If those structures no longer give it enough information to locate the message, it can’t open the file. It also can’t detect corruption within the contents of a stream that is still readable.

The 4 MiB setting in the example bounds only recovery’s search for lost directory sectors. It is not a cap on the size of the entire message; the compound file reader applies its own size and entry limits. Don’t use a higher recovery budget as a substitute for checking which fields and attachments you actually got back.

Check what was recovered

IsRecovered tells you whether container recovery ran, not whether a message is complete. Even with Recovery = true, a file that passes the strict container checks follows the normal read path and has IsRecovered = false. With a damaged container, CompoundFileRecovered records that recovery ran and CompoundFileDamage describes individual findings and their file offsets.

ReadEverything() visits recipients, attachments, and embedded messages. It doesn’t read all value streams. If your job needs a body or an attachment, read that value before collecting the final defect list. A StreamDamaged finding reports a stream that ended early; the bytes returned can be shorter than the declared size. Keep the original alongside any data you extract so you can distinguish recovered values from a complete source message.

Can Outlook’s Inbox Repair tool fix a .msg file?

Microsoft’s Inbox Repair tool works on Outlook data files (.pst and .ost), not an individual .msg saved from Outlook. To inspect a damaged MSG in C#, open that particular file with MsgFile.TryOpen. Mail.Open uses strict format detection and doesn’t opt into MSG container recovery.

All onemail recipes