Resolved Reading from Outlook POP PST-file ?

ksor

Active member
Joined
Jul 4, 2019
Messages
33
Programming Experience
10+
Just some background: Outlook is my local client for a GMAIL with 2 step logon and it's been working for years with some problems now and then.

The last days Outlook (which I has running all day) came up with the logon dialog as if it wants me to enter my password again !!!

When I set it up back then I used a 4 X 4 letter sequence generated by Google and I've ALLWAYS had problem find WHERE this generator was to find - the link I stored was pointing to something else when I should use the 4X4code again !

This time I could NOT find it so after some time I was forced to take a backup of my Outlook datafile and the re-install Outlook.

I managed to re-install Outlook and get it working again, BUT was NOT aware of the install is now IMAP format where my old Outlook was POP, so my backup was NOT useable !

I now wants to extract 1) folder with emails from my old INBOX and 2) my contacts - calender I just reconstruct in the new Outlook.

I'm using Microsoft Visual Studio Community 2022 (64-bit)and I found this C# code out there and changed the path to the PST-file to fit my needs:

C#:
using Microsoft.Office.Interop.Outlook;
using System;
using System.Activities;

namespace KS_Read_PST_1
{
    internal class Program {
        static void Main(string[] args) {
            Activity workflow1 = new Workflow1();
            WorkflowInvoker.Invoke(workflow1);

            string pstFilePath = @"C:\Users\kelds\POPkeld.soerensenS@gmail.com.pst";
            Application app = new Application();
            NameSpace outlookNs = app.GetNamespace("MAPI");                      

            // Add PST file (Outlook Data File) to Default Profile
            outlookNs.AddStore(pstFilePath);
            MAPIFolder rootFolder = outlookNs.Stores["items"].GetRootFolder();

            // Traverse through all folders in the PST file
            // TODO: This is not recursive
            Folders subFolders = rootFolder.Folders;
            foreach (Folder folder in subFolders) {
                Items items = folder.Items;
                foreach (object item in items) {
                    if (item is MailItem) {
                        // Retrieve the Object into MailItem
                        MailItem mailItem = item as MailItem;
                        Console.WriteLine("Saving message {0} ....", mailItem.Subject);
                        // Save the message to disk in MSG format
                        // TODO: File name may contain invalid characters [\ / : * ? " < > |]
                        mailItem.SaveAs(@"C:\Users\kelds\EXT\" + mailItem.Subject + ".msg", OlSaveAsType.olMSG);
                    }
                }
            }
            // Remove PST file from Default Profile
            outlookNs.RemoveStore(rootFolder);
        }
    }
}

IThe code runs til line 19 and then an exception comes up saying: An object was not found (tranlated from DANISH !)

1643121547746.png


What am I doing wrong ?
 
Please translate what the text after "Additional information:" .

I used to do MAPI programming many many years ago using C++ and talking to MAPI directly. I don't know how things have changed when you have to go through the Outlook Object Model. If you can iterate over the app.Stores to see what it contains, that would be great. Chances are that there isn't a MAPI store called "items".
 
I did translate but here is the whole truth and nothing but the truth:
Failing to perform the action, An object was not found.
 
Sorry. I missed that.

If you can enumrate the Stores collection it should give you a list of the valid names.

Out of curiosity, why not simply mount the .PST as a "Data File" to your Outlook profile so that you can get access to the messages and contacts there? Unless current Outlook has removed the functionality, I thought there was a way to import contacts from another PST in the profile. As for messages, I believe that Outlook still supports drag and drop. (Sorry. My work place won't let us create and use .PSTs so I can't try any of these.)
 
Sorry. I missed that.

If you can enumrate the Stores collection it should give you a list of the valid names.

Out of curiosity, why not simply mount the .PST as a "Data File" to your Outlook profile so that you can get access to the messages and contacts there? Unless current Outlook has removed the functionality, I thought there was a way to import contacts from another PST in the profile. As for messages, I believe that Outlook still supports drag and drop. (Sorry. My work place won't let us create and use .PSTs so I can't try any of these.)

THX for your reply and time - I have tried that BUT I the NEW installation is an IMAP and the OLD is POP - they CAN'T run together and UNFORTUNATELY there was NO warning for that when I re-installed Outlook.

It's so long time since I've worked with C# and too in the new VS2022 but I'll try your idea !
 
I've moved a copy of the backup of the PST-file to the desktop and too the folder where I want to store the extracted folder and mail.

I notice too that a datafile is presented in my running Outlook (IMAP) and the file I'm trying to extract from is a POP file - is that a problem ?

If I stop my running Outlook and try my extractor program again it fails in the line: outlookNs.AddStore(pstFilePath);

I forgot to take into account the FOLDERs too so now the code looks like this:

C#:
using Microsoft.Office.Interop.Outlook;
using System;
using System.Activities;

namespace KS_Read_PST_1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Activity workflow1 = new Workflow1();
            WorkflowInvoker.Invoke(workflow1);

            string pstFilePath = @"C:\Users\kelds\Desktop\POPkeld.soerensenS@gmail.com.pst";
            Application app = new Application();
            NameSpace outlookNs = app.GetNamespace("MAPI");

            // Add PST file (Outlook Data File) to Default Profile
            outlookNs.AddStore(pstFilePath);

            MAPIFolder rootFolder = outlookNs.Stores["items"].GetRootFolder();
            // Traverse through all folders in the PST file
            // TODO: This is not recursive
            Folders subFolders = rootFolder.Folders;
            foreach (Folder folder in subFolders)
            {
                // TODO: create the subfolder in EXT-mainfolder
                string fldName = folder.Name;
                Items items = folder.Items;
                foreach (object item in items)
                {
                    if (item is MailItem)
                    {
                        // Retrieve the Object into MailItem
                        MailItem mailItem = item as MailItem;
                        Console.WriteLine("Saving message {0} ....", mailItem.Subject);
                        // Save the message to disk in MSG format
                        // TODO: File name may contain invalid characters [\ / : * ? " < > |]
                        mailItem.SaveAs(@"C:\Users\kelds\desktop\EXT\" + fldName + @"\" + mailItem.Subject + ".msg", OlSaveAsType.olMSG);
                    }
                }
            }
            // Remove PST file from Default Profile
            outlookNs.RemoveStore(rootFolder);
        }
    }
}


Please somebody HELP me getting my inbox and subfolders with emails in there out of this PST-file and into a folder on the Desktop so I manually can load them into my re-installed Outlook in IMAP format !

I need your HELP so bad :sick: (Peter Green) !
 
Last edited:
A PST is just a storage file. It doesn't matter what was connected to it.

What problem are you running into?
 
A PST is just a storage file. It doesn't matter what was connected to it.

What problem are you running into?
Oh, this is driven me crazy - the major problem is - I think - that my COPY of the PST-file has a DIFFERENT password than my NOW running Outlook !
BEFORE I started getting wierd thing with my (POP) Outlook - 1-2 weeks ago - I had a 2-layer logon procedure on my GMAIL-account - this EXTRA/SECOND 'password' I should generate 'somewhere' at Googles site - through the years I had lots of problems finding WHERE - so I learned to save a link when I found it again, but EVERY time I got back to that link the site has changed and NOW I think Google has scraped this method - it was a 4X4 letter code I should enter in OUTLOOK as password.

I think my COPY of the PST has this 4X4 letter code as password !

The problem I ran into 1-2 weeks ago was I couldn't logon Outlook (the Outlook dialog for entering credentials came up all the time when Outlook was running!) I then took a backup (the one - a copy - I trying to access by C# today) and started re-installing Outlook but this time the 2 layer logon works by Google sending me a sort of text-message by PHONE where I have to answer that it's me trying to logon.
I think THAT's why I can't open my backup PST in newly re-installed Outlook !

And when I try using the C# code don't even HAVE any code for entering password and that's why the file CAN'T be opened - that's what the exception says - look here:

1643384323519.png


Is this non sense or ... ?
 
A dirty secret about the .PST file. Even though it is encrypted, it uses very weak encryption because of two things: First was that at the time it was being designed/written, there were severe limitations on what forms of encryption could be used for export outside the US. Second was that disk compression was very popular for Windows systems, so the encryption had to be compression friendly -- e.g. retain patterns in the data that data compression software would recognize as repeating and therefore be able to compress.

So all of that is just to give you hope that at worse case, some crypto analysis can be done by some competent cryptographers and be able to decrypt your PST.
 
Yes, your premise has some weight to it. Try a few Google searches about cracking .PST encryption. Most will return product offerings for data recovery, but there are also some that described how to recover your password, or a password that collides with your actual password. From what I recall, though, that may only be effective until Outlook 2007. After that point encryption got stronger again.
 
Yes, your premise has some weight to it. Try a few Google searches about cracking .PST encryption. Most will return product offerings for data recovery, but there are also some that described how to recover your password, or a password that collides with your actual password. From what I recall, though, that may only be effective until Outlook 2007. After that point encryption got stronger again.
I just found a free version that claims that the file is NOT protected by any password - now I'm really confused !

I can't open the file by the Outlook I have installed now (it's IMAP) - it says I don't have the "rights" to open the file !

Maybe I should try on a maschine with Outlook (POP) on it.

I MUST have cracked it open - I have contacts and inbox subfolders with lots of mails :cry:

The C# code DON'T have anything concerning entering af password - where should that be done ?
 
First step is not to use the Outlook Object Model. Call MAPILogonEx() API directly. My memory is really fuzzy. Forgive me. It's been over 20 years, and anesthesia amnesia doesn't help either. As I recall, the API has a password parameter, but you need to pass in NULL for that. It's when the profile that you choose to open has a .PST that requires a password is when a prompt comes up (if you pass logon flags saying allow UI).

This topic is wandering far away from C# and is more of a MAPI programming topic. As an aside, accessing MAPI is much easier with C or C++ than with C#, even with the .NET Interop and P/Invoke.
 
I managed to crack the PST-file open by copying it to a mashine where Outlook is still installed with the POP-protokol/format !

I got my inbox subfolders with emails in them AND all my contacts out to import to my newly installed Outlook in IMAP-format !

I HATE Google and all their crappy security checks when they have something new they wants to feed into our mouth - I HATE them !

But THX to you all here !
 

Latest posts

Back
Top Bottom