Create a new Unicode Outlook mail store; ANSI and 4 KiB-page output aren’t supported.
dotnet add package RecursiveAI.OneMailRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMail.Mapi; using RecursiveAI.OneMail.Pst.Messaging; using var store = PstBuilder.Create("archive.pst"); var inbox = store.IpmSubtree.CreateFolder("Inbox"); inbox.AddMessage(new MapiMessage()); store.Complete();
Writes archive.pst with an Inbox and one blank message; the snippet prints nothing.
Unzip the example, then dotnet run.
Full program
The full program supplies all its own input: subject “Delivery ready”, body “The package is ready.” and a UTF-8 attachment named note.txt containing “Packed today.” MAPI is the property model used for the message and attachment. The hexadecimal property IDs identify class, subject, body, attachment method, filename and attachment bytes.
using System.Text; using RecursiveAI.OneMail.Mapi; using RecursiveAI.OneMail.Pst.Messaging; var message = new MapiMessage(); message.Properties.Add(Text(0x001A, "IPM.Note")); message.Properties.Add(Text(0x0037, "Delivery ready")); message.Properties.Add(Text(0x1000, "The package is ready.")); var attachment = new MapiAttachment(message); attachment.Properties.Add(MapiProperty.Create(new MapiTag(0x3705, MapiType.Integer32), BitConverter.GetBytes(1))); attachment.Properties.Add(Text(0x3707, "note.txt")); attachment.Properties.Add(MapiProperty.Create(new MapiTag(0x3701, MapiType.Binary), Encoding.UTF8.GetBytes("Packed today."))); message.AddAttachment(attachment); using (var builder = PstBuilder.Create("archive.pst")) { builder.IpmSubtree.CreateFolder("Inbox").AddMessage(message); builder.Complete(); } if (!PstStore.TryOpen("archive.pst", out var store, out var defects)) throw new InvalidDataException(string.Join("; ", defects)); using (store) { var folder = store!.EnumerateFolders().Single(f => f.Name == "Inbox"); var saved = folder.EnumerateMessages().Single(); Console.WriteLine($"Folder: {folder.Name}"); Console.WriteLine($"Subject: {saved.Subject}"); Console.WriteLine($"Body: {saved.Body}"); Console.WriteLine($"Attachments: {saved.Attachments.Count}"); Console.WriteLine($"File: {saved.Attachments[0].FileName}"); using var data = saved.Attachments[0].OpenData()!; using var reader = new StreamReader(data); Console.WriteLine($"Data: {reader.ReadToEnd()}"); } static MapiProperty Text(ushort id, string value) => MapiProperty.Create(new MapiTag(id, MapiType.UnicodeString), Encoding.Unicode.GetBytes(value));
dotnet run prints:
Folder: Inbox Subject: Delivery ready Body: The package is ready. Attachments: 1 File: note.txt Data: Packed today.
Call Complete() before disposing the builder: stopping before completion leaves an empty header that readers refuse with BadMagic.
Variations
Create nested folders. This writes a second store with an Invoices folder under 2026:
using RecursiveAI.OneMail.Mapi; using RecursiveAI.OneMail.Pst.Messaging; using (var store = PstBuilder.Create("nested.pst")) { var year = store.IpmSubtree.CreateFolder("2026"); year.CreateFolder("Invoices").AddMessage(new MapiMessage()); store.Complete(); } if (!PstStore.TryOpen("nested.pst", out var back, out _)) throw new InvalidDataException(); using (back) Console.WriteLine(back!.EnumerateFolders() .Single(f => f.Name == "Invoices").Name);
InvoicesRelated recipes
The short answer
To create a PST file in C#, use PstBuilder.Create, create a folder under IpmSubtree, add MAPI messages and call Complete(). Dispose the builder before reopening the file. OneMail writes new Unicode PST stores with 512-byte pages; it does not write ANSI or 4 KiB-page stores.
The PST writing documentation distinguishes creating a store from editing an existing one. This recipe uses only the released OneMail package and creates a new file from synthetic data. It does not connect to an Outlook profile or download messages from an account.
How do I create a PST file?
The builder owns the destination while the store is being constructed. Create user folders under IpmSubtree, the “Top of Personal Folders” folder whose children Outlook shows. The root and other mandatory store structures are created by the builder; you do not need to construct them as messages.
AddMessage writes the message at once. The full program sets its class to IPM.Note, supplies a subject and body, and adds an attachment object with its filename and binary data. The builder also adds required properties by default. For example, the documentation lists message flags, size and timestamps among those additions. A minimal empty MapiMessage therefore gets an IPM.Note class even though the top snippet does not supply one.
Keep a source store open while handing its lazily loaded messages to AddMessage: attachment or property bytes may be read during the call. The synthetic example owns ordinary in-memory values and has no source-store lifetime to manage. The builder retains folder rows for completion, not the entire source message object as a future copy operation.
Call Complete() explicitly. Disposal alone is not completion. The header is written last, so a build stopped before completion leaves a file with a zero header that readers refuse. This is not a guarantee of crash consistency after an operating-system failure; the documentation explicitly separates that question from finishing a new store.
How do I create an Outlook archive?
Decide which messages belong in the archive, then add those messages to the chosen folders in a new store. The full program shows one folder and one attachment-bearing message; the variation shows a year folder with an Invoices subfolder. Folder creation does not imply a retention policy, automatic date filtering or removal of messages from their source.
The API can also create a store on a seekable, writable, empty stream. leaveOpen defaults to false; set it to true only when the calling code must keep using that stream after the builder is disposed. A forward-only network response is not a substitute for the required seekable destination.
Reopen the completed store before handing it to another part of your application. The program reads the folder, subject, body and attachment contents from disk. That catches mistakes such as assigning the wrong attachment property or putting a message in the wrong folder without needing a live mail account.
How do I create a new PST file in Outlook?
This code creates the file directly; it does not drive Outlook’s user interface. The project’s documentation records checks against Outlook 16.0.20527 and its repair tool, but those checks are scoped to the stores described there, not every Outlook version or every possible message.
The documented open questions include files over 2 GB and Outlook-only behavior for some item settings. ANSI and 4 KiB-page stores are never written. Choose this workflow for the Unicode stores the builder actually produces, and retain source data when your deployment needs compatibility checks outside that documented scope.