Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I read an Outlook (2003/2007) PST file in C#?
    text
    copied!<p>Is it possible to read a .PST file using C#? I would like to do this as a standalone application, not as an Outlook addin (if that is possible). </p> <p>If have seen <a href="https://stackoverflow.com/questions/318727/pst-to-csv-file-conversion">other</a> <a href="https://stackoverflow.com/questions/486883/problem-in-releasing-memory-from-an-outlook-pst-file">SO</a> <a href="https://stackoverflow.com/questions/235231/how-to-avoid-outlook-security-alert-when-reading-outlook-message-from-c-program">questions</a> <a href="https://stackoverflow.com/questions/466716/reading-an-outlook-2003-ost-file">similar</a> to this mention <a href="http://www.mailnavigator.com/reading_ms_outlook_pst_files.html" rel="noreferrer">MailNavigator</a> but I am looking to do this programmatically in C#.</p> <p>I have looked at the <a href="http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.aspx" rel="noreferrer">Microsoft.Office.Interop.Outlook</a> namespace but that appears to be just for Outlook addins. <a href="http://alioth.debian.org/projects/libpst/" rel="noreferrer">LibPST</a> appears to be able to read PST files, but this is in C (sorry Joel, I didn't <a href="http://www.joelonsoftware.com/articles/CollegeAdvice.html" rel="noreferrer">learn C before graduating</a>).</p> <p>Any help would be greatly appreciated, thanks!</p> <p><strong>EDIT:</strong> </p> <p>Thank you all for the responses! I accepted Matthew Ruston's response as the answer because it ultimately led me to the code I was looking for. Here is a simple example of what I got to work (You will need to add a reference to Microsoft.Office.Interop.Outlook):</p> <pre><code>using System; using System.Collections.Generic; using Microsoft.Office.Interop.Outlook; namespace PSTReader { class Program { static void Main () { try { IEnumerable&lt;MailItem&gt; mailItems = readPst(@"C:\temp\PST\Test.pst", "Test PST"); foreach (MailItem mailItem in mailItems) { Console.WriteLine(mailItem.SenderName + " - " + mailItem.Subject); } } catch (System.Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } private static IEnumerable&lt;MailItem&gt; readPst(string pstFilePath, string pstName) { List&lt;MailItem&gt; mailItems = new List&lt;MailItem&gt;(); 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[pstName].GetRootFolder(); // Traverse through all folders in the PST file // TODO: This is not recursive, refactor Folders subFolders = rootFolder.Folders; foreach (Folder folder in subFolders) { Items items = folder.Items; foreach (object item in items) { if (item is MailItem) { MailItem mailItem = item as MailItem; mailItems.Add(mailItem); } } } // Remove PST file from Default Profile outlookNs.RemoveStore(rootFolder); return mailItems; } } } </code></pre> <p><strong><em>Note:</strong> This code assumes that Outlook is installed and already configured for the current user. It uses the Default Profile (you can edit the default profile by going to Mail in the Control Panel). One major improvement on this code would be to create a temporary profile to use instead of the Default, then destroy it once completed.</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload