How to Work with Attachments in C#

How to Work with Attachments in C#

This guide shows how to add, read, and extract attachments from MSG files using Aspose.Email FOSS for .NET. File attachments, stream attachments, embedded messages, and inline image attachments are all covered using MapiAttachment.

Step 1 — Install the Package

dotnet add package Aspose.Email.Foss

Step 2 — Add File Attachments

using Aspose.Email.Foss.Msg;

var message = MapiMessage.Create("Report", "See attached files.");
message.SenderEmailAddress = "alice@example.com";
message.AddRecipient("bob@example.com", "Bob");

// From byte array
message.AddAttachment("report.pdf", File.ReadAllBytes("report.pdf"), "application/pdf");

// From stream
using var stream = File.OpenRead("photo.png");
message.AddAttachment("photo.png", stream, "image/png");

message.Save("with-attachments.msg");

Step 3 — Add Inline Images

Use the contentId parameter to embed an image referenced by HTML body:

var message = MapiMessage.Create("Newsletter", "");
message.HtmlBody = "<html><body><img src=\"cid:logo123\"></body></html>";

message.AddAttachment(
    "logo.png",
    File.ReadAllBytes("logo.png"),
    "image/png",
    "logo123");  // Content-ID matches cid: in HTML

message.Save("newsletter.msg");

Step 4 — Embed a Message as Attachment

Attach another MSG as an embedded message:

using var inner = MapiMessage.FromFile("original.msg");

var outer = MapiMessage.Create("Forwarded", "See the attached message.");
outer.AddEmbeddedMessageAttachment(inner, "original.msg", "application/vnd.ms-outlook");
outer.Save("forwarded.msg");

Step 5 — Read and Extract Attachments

using var message = MapiMessage.FromFile("with-attachments.msg");

foreach (var attachment in message.Attachments)
{
    Console.WriteLine($"Name: {attachment.Filename}");
    Console.WriteLine($"MIME: {attachment.MimeType}");
    Console.WriteLine($"Size: {attachment.Data.Length} bytes");
    Console.WriteLine($"Embedded: {attachment.IsEmbeddedMessage}");
    Console.WriteLine($"Storage: {attachment.IsStorageAttachment}");

    if (attachment.IsEmbeddedMessage)
    {
        // Access the embedded message
        Console.WriteLine($"  Subject: {attachment.EmbeddedMessage!.Subject}");
    }
    else
    {
        // Save to disk
        File.WriteAllBytes(attachment.Filename!, attachment.Data);
    }
}

Step 6 — Stream-Based Reading

Use OpenRead() for large attachments to avoid loading everything into memory:

foreach (var attachment in message.Attachments)
{
    if (!attachment.IsEmbeddedMessage)
    {
        using var readStream = attachment.OpenRead();
        using var fileStream = File.Create(attachment.Filename!);
        readStream.CopyTo(fileStream);
    }
}

Complete Example

using Aspose.Email.Foss.Msg;

// Create message with mixed attachments
var msg = MapiMessage.Create("Mixed Attachments", "Demo");
msg.SenderEmailAddress = "demo@example.com";
msg.AddRecipient("test@example.com", "Tester");

// File attachment
msg.AddAttachment("data.csv", File.ReadAllBytes("data.csv"), "text/csv");

// Embedded message
using var embedded = MapiMessage.FromFile("inner.msg");
msg.AddEmbeddedMessageAttachment(embedded, "inner.msg", "application/vnd.ms-outlook");

msg.Save("mixed.msg");
Console.WriteLine($"Attachments: {msg.Attachments.Count}"); // 2

See Also

 English