MboxWriter Class

Writes messages in an mbox file. The lines end with LF, as in the mbox files of Unix mail tools.

public sealed class MboxWriter : IDisposable

Example

using (var writer = MboxWriter.Create(path, MboxFormat.Mboxrd))
{
    var message = new MimeMessage { Subject = "Hello", From = [new MailboxAddress("Alice", "alice@example.com")] };
    var body = new MimePart();
    body.SetText("From the start of a line, escaped as >From in the file.");
    message.Body = body;
    writer.Write(message, date: new DateTimeOffset(2026, 1, 15, 9, 0, 0, TimeSpan.Zero));
}

var entry = MboxReader.ReadMessages(path).Single();
Assert.Equal("From alice@example.com Thu Jan 15 09:00:00 2026", entry.FromLine);
Assert.StartsWith("From the start of a line", entry.Message.TextBody, StringComparison.Ordinal);
Implements

Constructors

NameDescription
MboxWriter(Stream, MboxFormat, bool)Creates a writer that writes at the current position of a stream, such as the end of an existing file.

MboxWriter(Stream, MboxFormat, bool)

MboxWriter.cs

Creates a writer that writes at the current position of a stream, such as the end of an existing file.

public MboxWriter(Stream stream, MboxFormat format = MboxFormat.Mboxrd, bool leaveOpen = false)

Parameters

stream Stream
The stream.
format MboxFormat
The variant of the format.
leaveOpen bool
true to leave the stream open when the writer is disposed.

Properties

NameDescription
FormatGets the variant of the format.

Gets the variant of the format.

public MboxFormat Format { get; }

Property value

MboxFormat

Methods

NameDescription
Create(string, MboxFormat, bool)Creates a file, or appends to an existing one.
Dispose()Flushes the stream, and closes it unless the writer leaves it open.
Flush()Flushes the stream.
Write(MimeMessage, string?, DateTimeOffset?)Writes a message: its From line, its lines escaped as the format requires, then a blank line.
Write(ReadOnlySpan<byte>, string?, DateTimeOffset)Writes the bytes of a message.

Create(string, MboxFormat, bool)

MboxWriter.cs

Creates a file, or appends to an existing one.

public static MboxWriter Create(string path, MboxFormat format = MboxFormat.Mboxrd, bool append = false)

Parameters

path string
The path of the file.
format MboxFormat
The variant of the format.
append bool
true to add the messages at the end of an existing file.

Returns

MboxWriter The writer.

Dispose()

MboxWriter.cs

Flushes the stream, and closes it unless the writer leaves it open.

public void Dispose()

Flushes the stream.

public void Flush()

Write overloads

Write(MimeMessage, string?, DateTimeOffset?)

MboxWriter.cs

Writes a message: its From line, its lines escaped as the format requires, then a blank line.

public void Write(MimeMessage message, string? sender = null, DateTimeOffset? date = null)
Parameters
message MimeMessage
The message.
sender string?
The address of the envelope sender, or null for the address of the first From address of the message.
date DateTimeOffset?
The date of the From line, or null for the date of the message, or the current date.

Write(ReadOnlySpan<byte>, string?, DateTimeOffset)

MboxWriter.cs

Writes the bytes of a message.

public void Write(ReadOnlySpan<byte> content, string? sender, DateTimeOffset date)
Parameters
content ReadOnlySpan<byte>
The bytes of the message, without a From line.
sender string?
The address of the envelope sender.
date DateTimeOffset
The date of the From line.