PstWriter Class

Writes a new Outlook data file. The items are written when they are added; the folders, the indexes and the header when the writer is completed.

public sealed class PstWriter : IDisposable

Remarks

The writer creates new files: it does not modify an existing file. Use MapiMessageBuilder.FromMessage to copy items from another file.

Example

using (var writer = PstWriter.Create(path, new PstWriterOptions { DisplayName = "Archive" }))
{
    var inbox = writer.RootFolder.CreateFolder("Inbox", "IPF.Note");
    var message = new MapiMessageBuilder
    {
        Subject = "Hello",
        Body = "Hello from Meziantou.Framework.Email",
        SenderName = "Alice",
        SenderEmailAddress = "alice@example.com",
        SenderAddressType = "SMTP",
        ClientSubmitTime = new DateTime(2026, 1, 15, 9, 0, 0, DateTimeKind.Utc),
    };
    message.Recipients.Add(new MapiRecipientBuilder("Bob", "bob@example.com"));
    message.Attachments.Add(MapiAttachmentBuilder.FromContent("notes.txt", "Some notes"u8.ToArray(), "text/plain"));
    inbox.AddItem(message);

    var contacts = writer.RootFolder.CreateFolder("Contacts", "IPF.Contact");
    contacts.AddItem(new MapiContactBuilder { GivenName = "Bob", Surname = "Smith", Email1Address = "bob@example.com" });
}

using var file = PstFile.Open(path);
var item = file.GetFolder("Inbox")!.EnumerateItems().Single();
Assert.Equal("Hello", item.Subject);
Assert.Equal("bob@example.com", item.Recipients.Single().EmailAddress);
Assert.Equal("notes.txt", item.Attachments.Single().Name);
Implements

Properties

NameDescription
DeletedItemsGets the Deleted Items folder.
RootFolderGets the top of the folder hierarchy ("Top of Personal Folders"), the folder that contains the folders Outlook shows.

DeletedItems

PstWriter.cs

Gets the Deleted Items folder.

public PstWriterFolder DeletedItems { get; }

Property value

PstWriterFolder

RootFolder

PstWriter.cs

Gets the top of the folder hierarchy ("Top of Personal Folders"), the folder that contains the folders Outlook shows.

public PstWriterFolder RootFolder { get; }

Property value

PstWriterFolder

Methods

NameDescription
Complete()Writes the folders, the indexes and the header. The file is complete and valid after this call.
Create(Stream, PstWriterOptions?, bool)Creates a file in a stream.
Create(string, PstWriterOptions?)Creates a file.
Dispose()Completes the writer when it is not completed, and closes the stream unless it was created with leaveOpen.

Complete()

PstWriter.cs

Writes the folders, the indexes and the header. The file is complete and valid after this call.

public void Complete()

Exceptions

InvalidOperationException
The writer is already completed, or the content is too large for the format.

Create overloads

Create(Stream, PstWriterOptions?, bool)

PstWriter.cs

Creates a file in a stream.

public static PstWriter Create(Stream stream, PstWriterOptions? options = null, bool leaveOpen = false)
Parameters
stream Stream
A writable and seekable stream. The file starts at position 0.
options PstWriterOptions?
The options of the writer, or null for the default options.
leaveOpen bool
true to leave the stream open when the writer is disposed.
Returns

PstWriter The writer.

Exceptions
ArgumentException
The stream is not writable or not seekable.

Create(string, PstWriterOptions?)

PstWriter.cs

Creates a file.

public static PstWriter Create(string path, PstWriterOptions? options = null)
Parameters
path string
The path of the file. An existing file is overwritten.
options PstWriterOptions?
The options of the writer, or null for the default options.
Returns

PstWriter The writer.

Dispose()

PstWriter.cs

Completes the writer when it is not completed, and closes the stream unless it was created with leaveOpen.

public void Dispose()