---
title: "Write an Outlook .msg file from a MAPI message in C#"
url: https://recursiveai.org/onemail/cookbook/write-outlook-msg-from-mapi-message/
description: "Write an Outlook .msg file in C# from an in-memory MapiMessage. Set the subject and body, save with MsgWriter, and read it back without Outlook or COM."
version: "1.0.1"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMail) · [GitHub](https://github.com/recursive-ai-oss/onemail-csharp) · [Docs](https://recursiveai.org/onemail/docs/) · [Download example (.zip)](https://recursiveai.org/onemail/cookbook/write-outlook-msg-from-mapi-message/write-outlook-msg-from-mapi-message.zip)

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

```shell
dotnet add package RecursiveAI.OneMail
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onemail/cookbook/write-outlook-msg-from-mapi-message/write-outlook-msg-from-mapi-message.zip), 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.

```csharp
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:

```csharp
byte[] bytes = MsgWriter.ToArray(message);
Console.WriteLine($"MSG bytes: {bytes.Length}");
```

```text
MSG bytes: 3584
```

## Related recipes

[Read an Outlook .msg file](https://recursiveai.org/onemail/cookbook/read-msg-file/) · [Convert MSG to EML](https://recursiveai.org/onemail/cookbook/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](https://recursiveai.org/onemail/docs/mapi/), 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](https://recursiveai.org/onemail/docs/msg/) 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](https://recursiveai.org/onemail/cookbook/convert-msg-to-eml/) when the next tool expects MIME. This recipe uses the normal message writer, not the option that makes an Outlook template (`.oft`).
