MSGReader Documentation

repository·master·Indexed 19 days ago

https://github.com/sicos1977/msgreader

A .NET assembly for reading Outlook MSG and EML (Mime 1.0) files. It supports Outlook objects including E-mails, Appointments, Tasks, Contacts, and Sticky notes. Features include support for HTML embedded in RTF, charset detection via the UTF.Unknown package, attachment manipulation, and COM visibility for use in VB6 and VBScript.

Tokens
1.2K
Snippets
6
Records
6
Agent score
19%

What's inside MSGReader

  1. Use MSGReader from COM-based languages (VB6/VBScript)

    master

    MSGReader can be used in COM-based environments after registering the assembly.

    1. Compile the code and register the COM-visible assembly using: Regasm.exe /codebase MsgReader.dll
    2. Instantiate the reader using CreateObject.
    dim msgreader
    
    set msgreader = createobject("MsgReader.Reader")
    msgreader.ExtractToFolderFromCom "the msg file to read", "the folder where to place the extracted files"
  2. Configure Charset Detection Confidence Level

    master

    When MSG files contain HTML encapsulated in RTF, multiple languages might cause encoding issues. MSGReader uses the UTF.Unknown package to detect character encodings. You can adjust the CharsetDetectionEncodingConfidenceLevel property on the Reader or Message class to control the detection threshold.

    • Default value: 0.90f
    • Recommendation: Avoid values lower than 0.70f as they likely produce incorrect results.
    // Set the threshold for encoded string detection
    // Default is 0.90f
    message.CharsetDetectionEncodingConfidenceLevel = 0.90f;
  3. Delete an attachment from an Outlook message

    master

    You can manipulate an MSG file by removing attachments. To do this, open the message with FileAccess.ReadWrite, call DeleteAttachment on the specific attachment object, and then call Save to write the changes to a new file.

    var outlook = new Storage.Message(fileName, FileAccess.ReadWrite);
    outlook.DeleteAttachment(outlook.Attachments[0]);
    outlook.Save("d:\\deleted.msg");
  4. Read properties from an Outlook (eml) message

    master

    To read Mime 1.0 (.eml) files, use MsgReader.Mime.Message.Load(FileInfo). You can access headers (including To recipients and Subject) and extract the text or HTML body. Note that body content is returned as byte arrays, so you should decode them using an encoding like System.Text.Encoding.UTF8.

    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...
  5. Read properties from an Outlook (msg) message

    master

    Use the MsgReader.Outlook.Storage.Message class to load an .msg file and access its properties such as sender, sent date, recipients, subject, and body content. You can retrieve specific recipient lists using GetEmailRecipients with a RecipientType (e.g., To or Cc).

    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...
    }