Recursive AI

Create email replies and forwards in C#

OneMail is a free, open-source email library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Create reply, reply-all and forward drafts from a local message. Nothing is sent.

Terminal
dotnet add package RecursiveAI.OneMail

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

original.eml is included in the zip. If you paste the snippet instead, save the input message shown below as original.eml next to Program.cs.

C#
using RecursiveAI.OneMail;
using RecursiveAI.OneMail.Composition;
 
var mail = MimeMailItem.Parse(
    File.ReadAllBytes("original.eml"));
var drafts = new[] { mail.CreateReply(),
    mail.CreateReplyAll(),
    mail.CreateForward() };
for (int i = 0; i < drafts.Length; i++)
    File.WriteAllBytes($"draft-{i}.eml",
        drafts[i].Message.ToArray());
Console.WriteLine("Saved 3 EML drafts.");
Text
Saved 3 EML drafts.

Unzip the example, then dotnet run.

The input message (included in the zip) is:

Text
From: Ann <ann@example.org>
To: Me <me@example.org>, Bob <bob@example.org>
Cc: Cy <cy@example.org>
Subject: Plans
Message-ID: <plans@example.org>
References: <start@example.org>
Content-Type: text/plain; charset=utf-8
 
Meet on Friday.

The full program

The full program adds your sender address and body text, removes your address from reply-all, and prints each draft’s recipients and threading headers.

C#
using RecursiveAI.OneMail;
using RecursiveAI.OneMail.Composition;
using RecursiveAI.OneMail.Mime;
 
var original = MimeMailItem.Parse(File.ReadAllBytes("original.eml"));
var options = new CompositionOptions
{
    From = new MailAddress("Me", "me@example.org"),
    OwnAddresses = ["me@example.org"],
    Text = "Thanks, I’ll be there."
};
var reply = original.CreateReply(options);
var all = original.CreateReplyAll(options);
var forward = original.CreateForward(options);
Save("reply.eml", reply);
Save("reply-all.eml", all);
Save("forward.eml", forward);
 
static void Save(string path, ComposedMessage composed)
{
    File.WriteAllBytes(path, composed.Message.ToArray());
    Console.WriteLine($"{path}: {composed.Subject}");
    Console.WriteLine("  To: " + string.Join(", ", composed.To.Select(x => x.Address)));
    Console.WriteLine("  Cc: " + string.Join(", ", composed.Cc.Select(x => x.Address)));
    foreach (var name in new[] { "In-Reply-To", "References" })
        Console.WriteLine($"  {name}: {composed.Message.Headers.GetFirst(name)?.Value ?? "(none)"}");
}

dotnet run prints:

Text
reply.eml: Re: Plans
  To: ann@example.org
  Cc: 
  In-Reply-To: <plans@example.org>
  References: <start@example.org> <plans@example.org>
reply-all.eml: Re: Plans
  To: ann@example.org, bob@example.org
  Cc: cy@example.org
  In-Reply-To: <plans@example.org>
  References: <start@example.org> <plans@example.org>
forward.eml: Fwd: Plans
  To: 
  Cc: 
  In-Reply-To: (none)
  References: (none)

Check Defects and Unaddressable on each result for repaired headers and recipients without an SMTP address; file read/write errors propagate to the caller.

Variations

Forward the original as an attachment. Add this after the three Save calls in the full program:

C#
var attached = original.CreateForward(new CompositionOptions
{
    ForwardStyle = ForwardStyle.Attachment
});
File.WriteAllBytes("attached.eml", attached.Message.ToArray());
Console.WriteLine(attached.Subject);
Text
Fwd: Plans

The short answer

Parse the original with MimeMailItem.Parse, then call CreateReply, CreateReplyAll or CreateForward. Save the resulting ComposedMessage.Message as EML with ToArray(). These operations compose a new message; they neither change the original nor connect to a mail server.

The example deliberately uses a local EML file rather than an inbox. You can reproduce the recipient and header behavior without an account, credentials or network access. Its forward has no recipient: choosing who receives a forwarded message is separate from quoting the original. The composition documentation describes the same operations over the shared mail view, including messages read from Outlook stores.

How do I create an email reply in C#?

A reply is not just a new message with “Re:” added. The original’s Message-ID becomes In-Reply-To, and its existing References chain is extended with that ID. The full program shows both values, so you can inspect the relationship without relying on a mail client’s conversation display. When the original has no message ID, OneMail reports ParentHasNoMessageId and cannot write that parent into In-Reply-To.

Recipient selection follows the original’s reply headers. For MIME messages, Mail-Reply-To takes precedence, then Reply-To, then From. Supply your own addresses in CompositionOptions.OwnAddresses so that replies to messages you sent can target the people you wrote to instead of yourself. From supplies the new draft’s sender; Text supplies the response text above the quotation.

Subjects are normalized before the new prefix is added. Recognized leading reply and forward prefixes are stripped, so replying to an existing reply does not keep stacking “Re:”. A colon in an unrelated word, such as “Status:”, is not automatically a reply prefix. The recognized labels and override options are listed in the composition docs.

How do I create a reply-all or forward in C#?

Reply-all includes the reply target and the original’s To and Cc recipients, removes your own addresses, and de-duplicates addresses ignoring case. An address already in To is not repeated in Cc. For mailing-list messages, Mail-Followup-To can replace that recipient list. Inspect the returned To and Cc rather than constructing your own list from the original headers.

Forwarding has a different purpose. The default inline forward quotes the original; the attachment variation carries it as a message attachment. For ordinary MIME originals without a conversation index, a forward starts no reply thread and has neither In-Reply-To nor References. For Outlook originals, or originals already carrying a conversation index, the documented behavior preserves reply-style threading for forwards too. The absence of those headers in this sample is therefore intentional, not a lost reply chain.

Composition is not delivery. The files printed here are drafts you can inspect or hand to a separate sending workflow. Before sending, choose the forward recipient, review any reported defects, and check the composed body and attachments.

All onemail recipes