Read Outlook MSG Files in C#

To read an MSG file in C#, you can use the Microsoft.Office.Interop.Outlook assembly. This assembly provides a set of classes that can be used to interact with Microsoft Outlook.

To read an MSG file using the Microsoft.Office.Interop.Outlook assembly, you must first create an instance of the Microsoft.Office.Interop.Outlook.Application class. Once you have created an instance of the Microsoft.Office.Interop.Outlook.Application class, you can call the CreateItemFromTemplate method to create a new Outlook mail item from the MSG file.

Using Microsoft.Office.Interop.Outlook

The following code shows how to read an MSG file using the Microsoft.Office.Interop.Outlook assembly:

using Microsoft.Office.Interop.Outlook; public class Example { public static void Main() { // Create an instance of the Outlook application class. Application outlookApp = new Application(); // Create a new Outlook mail item from the MSG file. MailItem mailItem = outlookApp.CreateItemFromTemplate("C:\\path\\to\\msg\\file.msg"); // Read the properties of the mail item. string subject = mailItem.Subject; string body = mailItem.Body; // Close the mail item. mailItem.Close(OlSaveMode.olDoNotSave); // Release the Outlook application object. outlookApp.Quit(); } }

This code will read the subject and body of the MSG file and display them to the console.

You can also use the Microsoft.Office.Interop.Outlook assembly to read other properties of the MSG file, such as the sender, recipients, and attachments.

Here are some other examples of how to read an MSG file in C#:

Read the sender of the MSG file

string sender = mailItem.Sender.EmailAddress;

Read the recipients of the MSG file

foreach (Recipient recipient in mailItem.Recipients) { string recipientEmailAddress = recipient.EmailAddress; }

Read the attachments of the MSG file

foreach (Attachment attachment in mailItem.Attachments) { string attachmentFileName = attachment.FileName; }

By using the Microsoft.Office.Interop.Outlook assembly, you can read all of the properties of an MSG file and use them in your application.

Using Aspose.Email

There are also a number of third-party libraries available that can be used to read MSG files in C#. One popular library is called Aspose.Email. To use Aspose.Email to read an MSG file, you can use the following code:

using Aspose.Email; public class Example { public static void Main() { // Create a new MailMessage object from the MSG file. MailMessage mailMessage = MailMessage.Load("C:\\path\\to\\msg\\file.msg"); // Read the subject and body of the mail message. string subject = mailMessage.Subject; string body = mailMessage.Body; // Display the subject and body of the mail message to the console. Console.WriteLine(subject); Console.WriteLine(body); } }

This code will read the subject and body of the MSG file and display them to the console.

Conclusion

Which method you use to read MSG files in C# depends on your specific needs. If you only need to read a few MSG files, then using the Microsoft.Office.Interop.Outlook assembly is the simplest way to do it. However, if you need to read a lot of MSG files or if you need to read MSG files from a variety of different sources, then using a third-party library such as Aspose.Email is the more flexible approach.