Table of Contents

Example: Reading a pst / ost file from the local file system

using Encryptomatic.MailDex.Lib.PST.Messaging;
using Encryptomatic.MailDex.Lib.Readers;
using Encryptomatic.MailDex.Lib.License;
using Encryptomatic.MailDex.Lib.Messaging;
using Encryptomatic.MailDex.Lib.Messaging.Calendar;
using Encryptomatic.MailDex.Lib.Messaging.Contacts;

string activationKey = "YOUR-ACTI-KEYY";
string pstFileLocation = @"C:\test\test_file.pst";
string attachmentSaveFolderPath = @"C:\test\save_atts_to";




//EMailLibrary license
LicenseManager.LicenseInfo = new LicenseInfo() { LicenseKey = activationKey };


using (IReader reader = ReaderFactory.GetReader(pstFileLocation))
{

    //Loop recursively through mail folder tree.
    Stack<Folder> foldersStack = new Stack<Folder>();
    foldersStack.Push(reader.RootFolder);

    while (foldersStack.Count > 0)
    {
        Folder current = foldersStack.Pop();

        //Loop through messages in the sent folder.
        if (current.Name == "Sent Items")
        {
            IEnumerable<Message> msgs = reader.ReadMessages(current);
            foreach (Message msg in msgs)
            {
                //Display some of Message properties.
                Console.WriteLine($"{msg.EntryID} / {msg.Subject} / {msg.From} / {msg.DeliveryTime}");

                if (msg.HasAttachments)
                {
                    foreach (Attachment attachment in msg.Attachments)
                    {
                        if (attachment.AttachExtension == ".jpg")
                        {
                            using (FileStream fs = new FileStream(Path.Combine(attachmentSaveFolderPath, attachment.SafeAttachmentFileName), FileMode.Create, FileAccess.Write, FileShare.None))
                                reader.WriteAttachment(attachment, fs);
                        }
                    }
                }
            }
        }
        //Calendar folder usually contains Appointment items. Below will read those items as Appointment class instances. 
        else if (current.Name == "Calendar") {
            IEnumerable<Message> msgs = reader.ReadMessages(current);
            foreach (Message msg in msgs)
            {
                if (msg.MessageClass == "IPM.Appointment") { 
                    Appointment appointment = msg.AsAppointment();
                    Console.WriteLine(appointment.RecurrencePatternString);

                }
            }
        }
        //Same applies to Tasks and Contacts folders.
        else if (current.Name == "Tasks")
        {
            IEnumerable<Message> msgs = reader.ReadMessages(current);
            foreach (Message msg in msgs)
            {
                if (msg.MessageClass == "IPM.Task")
                {
                    Encryptomatic.MailDex.Lib.Messaging.Tasks.Task task = msg.AsTask();
                    Console.WriteLine(task.PercentComplete);

                }
            }
        }
        else if (current.Name == "Contacts")
        {
            IEnumerable<Message> msgs = reader.ReadMessages(current);
            foreach (Message msg in msgs)
            {
                if (msg.MessageClass == "IPM.Contact")
                {
                    Contact contact = msg.AsContact();
                    Console.WriteLine(contact.BusinessTelephoneNumber);

                }
            }
        }

        if (current.SubFolders == null)
            continue;

        foreach (Folder subFolder in current.SubFolders)
        {
            foldersStack.Push(subFolder);
        }
    }


}