---
title: "Extract an embedded email from an EML file in C#"
url: https://recursiveai.org/onemail/cookbook/read-embedded-email-message/
description: "Open an EML file in C#, find its embedded message attachment, read the inner subject, and save the attached message as a separate EML file with OneMail."
version: "1.0.1"
---

# Extract an embedded email from an EML file in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMail) · [GitHub](https://github.com/recursive-ai-oss/onemail-csharp) · [Docs](https://recursiveai.org/onemail/docs/) · [Download example (.zip)](https://recursiveai.org/onemail/cookbook/read-embedded-email-message/read-embedded-email-message.zip)

Save the message inside `forward.eml` (in the zip) as a separate EML file.

```shell
dotnet add package RecursiveAI.OneMail
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneMail;

using var file = Mail.Open("forward.eml");
var outer = file.Enumerate().Single();
var part = outer.Attachments.FirstOrDefault(
    a => a.Embedded is not null);
if (part?.Embedded is not { } inner)
    throw new InvalidDataException(
        "No embedded message.");

using var input = part.Open();
using var output = File.Create("attached.eml");
input.CopyTo(output);
Console.WriteLine("Subject: " + inner.Subject);
Console.WriteLine("Saved attached.eml");
```

```text
Subject: Pickup at noon
Saved attached.eml
```

Here’s `forward.eml`. The outer `Content-Type` names the `sample` boundary. The second `--sample` line starts the attached message part.

```text
From: mira@example.com
To: dev@example.com
Subject: Forwarded as an attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="sample"

--sample
Content-Type: text/plain; charset=utf-8

See the attached note.
--sample
Content-Type: message/rfc822
Content-Disposition: attachment; filename="note.eml"

From: alex@example.com
To: mira@example.com
Subject: Pickup at noon
Content-Type: text/plain; charset=utf-8

Use the south entrance.
--sample--
```

If no attachment has an `Embedded` item, the example throws `InvalidDataException` before it creates `attached.eml`.

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/onemail/cookbook/read-embedded-email-message/read-embedded-email-message.zip) and `dotnet run`.

## Variations

Read the inner plain-text body without saving it. Keep the code through the `if` check and replace the file-writing and printing lines with:

```csharp
Console.WriteLine(inner.TextBody?.ReadText().Trim());
```

```text
Use the south entrance.
```

## Related recipes

- [Extract ordinary email attachments](https://recursiveai.org/onemail/cookbook/extract-attachments/)
- [Read an EML file](https://recursiveai.org/onemail/cookbook/parse-eml/)

## The short answer

Open the EML with `Mail.Open` and enumerate its single item. In that item’s `Attachments`, look for an attachment whose `Embedded` property is not null. The property gives you another `IMailItem`, so you can read the attached message’s subject or body directly. To save it instead, copy the stream from the attachment’s `Open()` method into an `.eml` file.

## What does `message/rfc822` mean in an attachment?

It’s the MIME type for a message carried inside another message ([RFC 2046, section 5.2.1](https://www.rfc-editor.org/rfc/rfc2046#section-5.2.1)). The outer `Subject` here is “Forwarded as an attachment”; the inner `Subject` is “Pickup at noon.” They come from separate sets of headers. The inner message can have its own body and attachments, and you read them through the same properties you used on the outer message. An inline forward that only quotes the old email in the body doesn’t have this structure. It won’t give you an `Embedded` item to extract.

## How do you extract the attached EML without including the outer message?

Copy from `part.Open()`, not from `forward.eml` itself and not from the outer item’s `TextBody`. The stream supplied for this `message/rfc822` part contains the inner message, so it begins with the inner headers, not with the `Content-Type: message/rfc822` line that introduced the attachment. Keep the outer container open while you read the attachment. The [shared view docs](https://recursiveai.org/onemail/docs/shared-view/#opening-things-mail) explain that items can read their bytes on first use, so an unread item can’t be used after you dispose the container.

The sample deliberately uses a fixed output path. `File.Create` replaces an existing `attached.eml`, so change the path before rerunning if you want to keep the previous file. `FileName` is the name recorded in the message, not a safe local path. The name is also not what makes an attachment embedded: a generic binary part named `note.eml` can have an inferred media type without providing an `Embedded` item.

## How do you see attachments inside a forwarded email?

Once you have the inner `IMailItem`, look at its `Attachments`, not the list on the original item. The outer list reports the attached email as one part; it doesn’t list that email’s own attachments as peers. If the outer email can contain several forwarded messages, the example’s `FirstOrDefault` selects just the first recognized one. To handle all of them, inspect each outer attachment for an `Embedded` item, then work with each inner message and its own attachment list.
