Tuesday 5 May 2020

MSGReader

https://github.com/Sicos1977/MSGReader

What is MSGReader

MSGReader is a C# .NET 4.5 and Standard 2.0 (supports C# .NET 4.6.1 and up) library to read Outlook MSG and EML (Mime 1.0) files. Almost all common object in Outlook are supported:
  • E-mail
  • Appointment
  • Task
  • Contact card
  • Sticky note
It supports all body types there are in MSG files, this includes:
  • Text
  • HTML
  • HTML embedded into RTF
  • RTF
MSGReader has only a few options to manipulate an MSG file. The only option you have is that you can remove attachments and then save the file to a new one.
If you realy want to write MSG files then see my MsgKit project on GitHub (https://github.com/Sicos1977/MsgKit)

Read properties from an Outlook (msg) message

using (var msg = new MsgReader.Outlook.Storage.Message("d:\\testfile.msg"))
{
        var from = msg.Sender;
        var sentOn = msg.SentOn;
        var recipientsTo = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.To, false, false);
        var recipientsCc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Cc, false, false);
        var subject = msg.Subject;
        var htmlBody = msg.BodyHtml;
        // etc...
}

Read properties from an Outlook (eml) message

var fileInfo = new FileInfo("d:\\testfile.eml");
var eml = MsgReader.Mime.Message.Load(fileInfo);

if (eml.Headers != null)
{
        if (eml.Headers.To != null)
        {
            foreach (var recipient in eml.Headers.To)
            {
                var to = recipient.Address;            
            }
        }
}

var subject = eml.Headers.Subject;

if (eml.TextBody != null)
{
        var textBody = System.Text.Encoding.UTF8.GetString(eml.TextBody.Body);
}

if (eml.HtmlBody != null)
{
        var htmlBody = System.Text.Encoding.UTF8.GetString(eml.HtmlBody.Body);
}

// etc...

Delete attachment from an Outlook message

This example deletes the first attachment
var outlook = new Storage.Message(fileName, FileAccess.ReadWrite);
outlook.DeleteAttachment(outlook.Attachments[0]);
outlook.Save("d:\\deleted.msg");

Translations

  • Kees van Spelde
    • English (US)
    • Dutch
  • Ronald Kohl
    • German
  • Yan Grenier (@ygrenier on GitHub)
    • French
  • xupefei
    • Simpl Chinese

Installing via NuGet

The easiest way to install MSGReader is via NuGet.
In Visual Studio's Package Manager Console, simply enter the following command:
Install-Package MSGReader

Side note

This project can also be used from a COM based language like VB script or VB6. To use it first compile the code and register the com visible assembly with the command:
Regasm.exe /codebase MsgReader.dll
After that you can call it like this:
dim msgreader

set msgreader = createobject("MsgReader.Reader")
msgreader.ExtractToFolderFromCom "the msg file to read", "the folder where to place the extracted files"

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive