How to Save Email Message to Disk in C#

In this step by step tutorial, we’ll explain how to save email message to disk in C#. The email message can be saved as EML or MSG file formats on the disk. We’ll be getting messages from Exchange email box, however you can get messages from other mail boxes as well and save to disk.

Steps to Save Email Message to Disk in C#

  1. Get Aspose.Email for .NET package from NuGet.org
  2. Include Aspose.Email.Clients.Exchange and Aspose.Email.Clients.Exchange.WebService namespaces
  3. Set license with the help of SetLicense method
  4. Provide Exchange Server account information
  5. Create an object of NetworkCredential class
  6. Create a new Exchange client object of EWSClient Class
  7. Loop through all the email messages read from the mail box
  8. Save each individual email message to EML or MSG file formats

By following the above simple steps, you can save email message to EML in C# code on the disk. In our previous topic, we explained how to send email in C# using Gmail SMTP server. However, in this topic, we’re using the Exchange server instead to fetch the emails and then save them to disk.

Code to Save Email Message to Disk in C#

using System;
using System.Net;
//Add reference to Aspose.Email for .NET API
//Use following namespaces to save email message to disk
using Aspose.Email.Clients.Exchange;
using Aspose.Email.Clients.Exchange.WebService;
namespace SaveEmailMessageToDisk
{
class Program
{
static void Main(string[] args)
{
//Set license before saving email message to disk
Aspose.Email.License AsposeEmailLicense = new Aspose.Email.License();
AsposeEmailLicense.SetLicense(@"c:\asposelicense\license.lic");
//setup account information
const string MailBoxUri = "https://outlook.office365.com/ews/exchange.asmx";
const string Domain = @"";
const string Username = @"username@outlook.com";
const string Password = @"userpassword";
NetworkCredential CredentiaDetails =
new NetworkCredential(Username, Password, Domain);
//create email client
IEWSClient EmailClient = EWSClient.GetEWSClient(MailBoxUri, CredentiaDetails);
try
{
//get mail box
ExchangeMailboxInfo MailBoxInfo = EmailClient.GetMailboxInfo();
//get list of all messages from the mail box
ExchangeMessageInfoCollection MessagesCollection =
EmailClient.ListMessages(EmailClient.MailboxInfo.InboxUri);
//Loop through email messages
int MessageCount = 1;
foreach (ExchangeMessageInfo MessageInfo in MessagesCollection)
{
string strMessageURI = MessageInfo.UniqueUri;
//get message details
Console.WriteLine("Subject: " + MessageInfo.Subject);
Console.WriteLine("From: " + MessageInfo.From.ToString());
Console.WriteLine("To: " + MessageInfo.To.ToString());
Console.WriteLine("Message ID: " + MessageInfo.MessageId);
Console.WriteLine("Unique URI: " + MessageInfo.UniqueUri);
//save each message to disk
EmailClient.SaveMessage(strMessageURI, "Messsage_" + MessageCount + ".eml");
EmailClient.SaveMessage(strMessageURI, "Messsage_" + MessageCount + ".msg");
MessageCount++;
}
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
}
}
}

In the above code, we’re saving email messages as .eml and .msg formats. Please note that in this code, we’re fetching all of the messages and saving them one by one in separate files. However, if your inbox has a lot of messages then you need to modify the approach accordingly.

 English