Recursive AI

Write an Outlook .msg file from a MAPI message in C#

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

Set the message’s MAPI properties (Outlook’s typed fields), then save with MsgWriter.Write.

Terminal
dotnet add package RecursiveAI.OneMail

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using System.Text;
using RecursiveAI.OneMail.Mapi;
using RecursiveAI.OneMail.Msg;
 
var message = new MapiMessage();
AddText(MapiPid.MessageClass, "IPM.Note");
AddText(MapiPid.Subject, "Meeting notes");
AddText(MapiPid.Body,
    "The next meeting is on Friday.");
MsgWriter.Write(message, "notes.msg");
Console.WriteLine("Wrote notes.msg");
 
void AddText(ushort id, string text) =>
    message.Properties.Add(MapiProperty.CreateOwned(
        new MapiTag(id, MapiType.UnicodeString),
        Encoding.Unicode.GetBytes(text)));

Unzip the example, then dotnet run.

The snippet saves an Outlook .msg file in the current directory:

Text
Wrote notes.msg

Full program

The full program also prints the writer’s findings, then opens notes.msg and reads its subject and plain-text body. Use an output path you’re willing to overwrite.

C#
using System.Text;
using RecursiveAI.OneMail.Mapi;
using RecursiveAI.OneMail.Msg;
 
var message = new MapiMessage();
AddText(MapiPid.MessageClass, "IPM.Note");
AddText(MapiPid.Subject, "Meeting notes");
AddText(MapiPid.Body, "The next meeting is on Friday.");
 
var result = MsgWriter.Write(message, "notes.msg");
Console.WriteLine("Wrote notes.msg");
foreach (var defect in result.Defects)
    Console.WriteLine($"{defect.Kind}: {defect.Detail}");
 
if (!MsgFile.TryOpen("notes.msg", out var file, out var defects))
    throw new InvalidDataException(string.Join(", ", defects));
using (file)
{
    Console.WriteLine($"Subject: {file.Message.Subject}");
    Console.WriteLine($"Body: {file.Message.Body}");
}
 
void AddText(ushort id, string text) =>
    message.Properties.Add(MapiProperty.CreateOwned(
        new MapiTag(id, MapiType.UnicodeString),
        Encoding.Unicode.GetBytes(text)));

dotnet run prints:

Text
Wrote notes.msg
PropertyAdjusted: PidTagStoreSupportMask got STORE_UNICODE_OK for Unicode strings
Subject: Meeting notes
Body: The next meeting is on Friday.

The PropertyAdjusted: PidTagStoreSupportMask line is a normal writer finding, not a failure.

If the model exceeds a writer limit, MsgWriter.Write throws MsgWriteException with the findings before writing any bytes.

Variations

Get the MSG bytes without writing a file. Replace the write and print calls at the top with these lines; message is the same in-memory model:

C#
byte[] bytes = MsgWriter.ToArray(message);
Console.WriteLine($"MSG bytes: {bytes.Length}");
Text
MSG bytes: 3584

Read an Outlook .msg file · Convert MSG to EML


The short answer

Create a MapiMessage, set the fields with MapiProperty values, and call MsgWriter.Write(message, "notes.msg"). If you already have a populated MapiMessage, you can pass it straight to Write. The call saves an Outlook message file; it doesn’t send the message or need a mailbox account.

How do I create an Outlook .msg file in C#?

An Outlook MSG file holds a single item. In OneMail’s MAPI model, each property’s tag identifies both the field and its type. For example, the program sets MapiPid.Subject with the UnicodeString type and supplies UTF-16 little-endian bytes via Encoding.Unicode. IPM.Note identifies the item as a mail message. The subject and body are enough for this example; they don’t supply sender or recipient information. If those fields matter to your application, populate them in the model before writing.

The MSG writer lays out the compound file from the model; you don’t build its streams by hand. The model can hold named properties, recipients, attachments, and embedded messages as well. If you’re reusing a model read from another file, keep its reader open until the write finishes: property values can be backed by streams that are read on demand.

Check the writer’s findings

The default writer adds or adjusts some fields that Outlook expects. In the example it adds the Unicode flag to the store support mask, which describes how strings are stored. That is a write finding, not a failed save. Other findings can indicate an omitted or changed value, so inspect result.Defects rather than assuming every field in the output matches the model. For an application that needs to decide whether to write at all, MsgWriter.Check plans the file and reports findings without writing it.

Is MSG the same as EML?

No. MSG stores Outlook’s MAPI properties in a binary compound file, whereas EML stores an Internet email message with headers and MIME parts. Renaming between .msg and .eml does not convert the contents. Use MSG when the downstream job needs that Outlook item model, and convert to EML when the next tool expects MIME. This recipe uses the normal message writer, not the option that makes an Outlook template (.oft).

All onemail recipes