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 : IDisposableRemarks
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
| Name | Description |
|---|---|
| DeletedItems | Gets the Deleted Items folder. |
| RootFolder | Gets the top of the folder hierarchy ("Top of Personal Folders"), the folder that contains the folders Outlook shows. |
DeletedItems
PstWriter.csRootFolder
PstWriter.csGets the top of the folder hierarchy ("Top of Personal Folders"), the folder that contains the folders Outlook shows.
public PstWriterFolder RootFolder { get; }Property value
Methods
| Name | Description |
|---|---|
| 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.csWrites 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.csCreates a file in a stream.
public static PstWriter Create(Stream stream, PstWriterOptions? options = null, bool leaveOpen = false)Parameters
streamStream- A writable and seekable stream. The file starts at position 0.
optionsPstWriterOptions?- The options of the writer, or
nullfor the default options. leaveOpenbooltrueto 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.csCreates a file.
public static PstWriter Create(string path, PstWriterOptions? options = null)Parameters
pathstring- The path of the file. An existing file is overwritten.
optionsPstWriterOptions?- The options of the writer, or
nullfor the default options.
Returns
PstWriter The writer.
Dispose()
PstWriter.csCompletes the writer when it is not completed, and closes the stream unless it was created with leaveOpen.
public void Dispose()