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 : IDisposableExample
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
| Name | Description |
|---|---|
| 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.csCreates 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
streamStream- The stream.
formatMboxFormat- The variant of the format.
leaveOpenbooltrueto leave the stream open when the writer is disposed.
Properties
| Name | Description |
|---|---|
| Format | Gets the variant of the format. |
Format
MboxWriter.csMethods
| Name | Description |
|---|---|
| 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.csCreates a file, or appends to an existing one.
public static MboxWriter Create(string path, MboxFormat format = MboxFormat.Mboxrd, bool append = false)Parameters
pathstring- The path of the file.
formatMboxFormat- The variant of the format.
appendbooltrueto add the messages at the end of an existing file.
Returns
MboxWriter The writer.
Dispose()
MboxWriter.csFlushes the stream, and closes it unless the writer leaves it open.
public void Dispose()Flush()
MboxWriter.csFlushes the stream.
public void Flush()Write overloads
Write(MimeMessage, string?, DateTimeOffset?)
MboxWriter.csWrites 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
messageMimeMessage- The message.
senderstring?- The address of the envelope sender, or
nullfor the address of the firstFromaddress of the message. dateDateTimeOffset?- The date of the
Fromline, ornullfor the date of the message, or the current date.
Write(ReadOnlySpan<byte>, string?, DateTimeOffset)
MboxWriter.csWrites the bytes of a message.
public void Write(ReadOnlySpan<byte> content, string? sender, DateTimeOffset date)Parameters
contentReadOnlySpan<byte>- The bytes of the message, without a
Fromline. senderstring?- The address of the envelope sender.
dateDateTimeOffset- The date of the
Fromline.