MaildirFolder Class

A Maildir folder: a directory with the subdirectories tmp, new and cur, and a file per message. The subfolders follow the Maildir++ convention: directories whose names start with a dot.

public sealed class MaildirFolder

Example

var folder = MaildirFolder.Create(path);
var message = new MimeMessage { Subject = "Hello" };
var body = new MimePart();
body.SetText("Hello from a Maildir folder.");
message.Body = body;

// A message without flags goes in "new", a message with flags in "cur" with the flags in its name
var added = folder.AddMessage(message);
Assert.True(added.IsNew);
var read = folder.SetFlags(added, MaildirFlags.Seen | MaildirFlags.Replied);
Assert.EndsWith("2,RS", read.FullPath, StringComparison.Ordinal);

var sent = folder.CreateFolder("Sent");
sent.AddMessage(message, MaildirFlags.Seen);
Assert.Equal(["Sent"], folder.EnumerateFolders().Select(subfolder => subfolder.Name));
Assert.Equal("Hello", folder.EnumerateMessages().Single().Load().Subject);

Properties

NameDescription
FullPathGets the path of the directory of the folder.
InfoSeparatorGets the character between the unique name of a message and its information when the folder writes a file name: :, or ! on Windows, where a file name cannot contain :. The reader accepts :, ! and ;.

Gets the path of the directory of the folder.

public string FullPath { get; }

Property value

string

InfoSeparator

MaildirFolder.cs

Gets the character between the unique name of a message and its information when the folder writes a file name: :, or ! on Windows, where a file name cannot contain :. The reader accepts :, ! and ;.

public static char InfoSeparator { get; }

Property value

char

Methods

NameDescription
AddMessage(MimeMessage, MaildirFlags)Adds a message: writes it in tmp, then moves it to new, or to cur when it has flags.
AddMessage(ReadOnlySpan<byte>, MaildirFlags)Adds the bytes of a message: writes them in tmp, then moves the file to new, or to cur when the message has flags.
Create(string)Creates a folder, or opens it when it exists.
CreateFolder(string)Creates a subfolder (Maildir++), or opens it when it exists.
Delete(MaildirMessage)Deletes the file of a message.
EnumerateFolders()Returns the subfolders (Maildir++): the subdirectories whose names start with a dot, such as .Sent or .Archive.2026.
EnumerateMessages()Returns the messages of the new and cur directories. The files whose names start with a dot are skipped.
Open(string)Opens a folder.
SetFlags(MaildirMessage, MaildirFlags)Sets the flags of a message: moves its file to cur, with the flags in its name.

AddMessage overloads

AddMessage(MimeMessage, MaildirFlags)

MaildirFolder.cs

Adds a message: writes it in tmp, then moves it to new, or to cur when it has flags.

public MaildirMessage AddMessage(MimeMessage message, MaildirFlags flags = MaildirFlags.None)
Parameters
message MimeMessage
The message.
flags MaildirFlags
The flags of the message.
Returns

MaildirMessage The message added.

AddMessage(ReadOnlySpan<byte>, MaildirFlags)

MaildirFolder.cs

Adds the bytes of a message: writes them in tmp, then moves the file to new, or to cur when the message has flags.

public MaildirMessage AddMessage(ReadOnlySpan<byte> content, MaildirFlags flags = MaildirFlags.None)
Parameters
content ReadOnlySpan<byte>
The bytes of the message.
flags MaildirFlags
The flags of the message.
Returns

MaildirMessage The message added.

Create(string)

MaildirFolder.cs

Creates a folder, or opens it when it exists.

public static MaildirFolder Create(string path)

Parameters

path string
The path of the directory.

Returns

MaildirFolder The folder.

CreateFolder(string)

MaildirFolder.cs

Creates a subfolder (Maildir++), or opens it when it exists.

public MaildirFolder CreateFolder(string name)

Parameters

name string
The name of the subfolder, such as Sent; the levels are separated by dots (Archive.2026).

Returns

MaildirFolder The subfolder.

Delete(MaildirMessage)

MaildirFolder.cs

Deletes the file of a message.

public void Delete(MaildirMessage message)

Parameters

message MaildirMessage
The message.

EnumerateFolders()

MaildirFolder.cs

Returns the subfolders (Maildir++): the subdirectories whose names start with a dot, such as .Sent or .Archive.2026.

public IEnumerable<(string Name, MaildirFolder Folder)> EnumerateFolders()

Returns

IEnumerable<(string Name, MaildirFolder Folder)> The names of the subfolders, without their first dot, and their folders.

EnumerateMessages()

MaildirFolder.cs

Returns the messages of the new and cur directories. The files whose names start with a dot are skipped.

public IEnumerable<MaildirMessage> EnumerateMessages()

Returns

IEnumerable<MaildirMessage> The messages.

Open(string)

MaildirFolder.cs

Opens a folder.

public static MaildirFolder Open(string path)

Parameters

path string
The path of the directory.

Returns

MaildirFolder The folder.

Exceptions

DirectoryNotFoundException
The directory does not exist, or has no cur or new subdirectory.

SetFlags(MaildirMessage, MaildirFlags)

MaildirFolder.cs

Sets the flags of a message: moves its file to cur, with the flags in its name.

public MaildirMessage SetFlags(MaildirMessage message, MaildirFlags flags)

Parameters

message MaildirMessage
The message.
flags MaildirFlags
The flags.

Returns

MaildirMessage The message, with its new path.