Quickstart

Quickstart

This page contains three self-contained examples you can copy into a .NET 8 console project and run immediately. Each example covers a core workflow of the library.

Prerequisites

dotnet new console -n EmailDemo
cd EmailDemo
dotnet add package Aspose.Email.Foss

Example 1 — Read an MSG File

Open an Outlook .msg file and print the subject, sender, recipients, and attachments.

using System.IO;
using Aspose.Email.Foss.Msg;

using var stream = File.OpenRead("sample.msg");
var message = MapiMessage.FromStream(stream);

Console.WriteLine($"Subject: {message.Subject}");
Console.WriteLine($"From: {message.SenderName} <{message.SenderEmailAddress}>");

foreach (var recipient in message.Recipients)
    Console.WriteLine($"  To: {recipient.DisplayName} <{recipient.EmailAddress}>");

foreach (var attachment in message.Attachments)
    Console.WriteLine($"  Attachment: {attachment.Filename} ({attachment.MimeType})");

Example 2 — Create and Save a Message

Build a complete email with sender, recipients, and an attachment, then write it to an MSG file.

using System.IO;
using Aspose.Email.Foss.Msg;

var message = MapiMessage.Create("Quarterly Update", "Please see the attached report.");
message.SenderName = "Alice";
message.SenderEmailAddress = "alice@example.com";

message.AddRecipient("bob@example.com", "Bob");
message.AddRecipient("carol@example.com", "Carol", MapiMessage.RecipientTypeCc);

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

message.Save("quarterly-update.msg");

Verify the result by reading it back:

using var msg = MapiMessage.FromFile("quarterly-update.msg");
Console.WriteLine(msg.Subject);           // Quarterly Update
Console.WriteLine(msg.Attachments.Count); // 1

Example 3 — Convert Between EML and MSG

Load a standard .eml file (RFC 5322 / MIME) and save it as Outlook MSG, then convert back.

using System.IO;
using Aspose.Email.Foss.Msg;

// EML → MSG
using var emlInput = File.OpenRead("incoming.eml");
var message = MapiMessage.LoadFromEml(emlInput);
message.Save("incoming.msg");

// MSG → EML (round-trip)
message.SaveToEml("roundtrip.eml");

Subject, body, HTML body, sender, recipients, and attachments are all preserved through the EML → MSG → EML round-trip.


Next Steps

 English