Can I read an Outlook (2003/2007) PST file in C#?
The Outlook Interop library is not just for addins. For example it could be used to write a console app that just reads all your Outlook Contacts. I am pretty sure that the standard Microsoft Outlook Interop library will let you read the mail - albeit it will probably throw a security prompt in Outlook that the user will have to click through.
EDITS: Actually implementing mail reading using Outlook Interop depends on what your definition of 'standalone' means. The Outlook Interop lib requires Outlook to be installed on the client machine in order to function.
// Dumps all email in Outlook to console window.
// Prompts user with warning that an application is attempting to read Outlook data.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookEmail
{
class Program
{
static void Main(string[] args)
{
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
foreach (Outlook.MailItem item in emailFolder.Items)
{
Console.WriteLine(item.SenderEmailAddress + " " + item.Subject + "\n" + item.Body);
}
Console.ReadKey();
}
}
}
How to get records of email and user information from outlook 2007 pst file?
in the line below you point it to a folder and not the actual file.
IEnumerable<MailItem> mailItems = readPst(@"C:\Users\Toseef Abbasi\AppData
\Local\Microsoft\Outlook", "Outltoseefabbasi@hotmail.com-0000000b");
shoud be like below where pstname.pst is the actual pst file.
IEnumerable<MailItem> mailItems = readPst(@"C:\Users\Toseef Abbasi\AppData
\Local\Microsoft\Outlook\pstname.pst", "Outltoseefabbasi@hotmail.com-0000000b");
Is there a way to import PST files into Outlook using C#?
You can do that the following way:
In your project, right click on "References" and add a reference to the assembly "Microsoft.Office.Interop.Outlook".
Then you can use the following code:
/// <summary>
/// Get a reference to an already running or a newly started Outlook instance
/// </summary>
Microsoft.Office.Interop.Outlook.Application GetOutlookApp()
{
Microsoft.Office.Interop.Outlook.Application app = null;
// Try to get running instance
try
{
app = Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
}
catch(Exception)
{
// Ignore exception when Outlook is not running
}
// When outlook was not running, try to start it
if(app == null)
{
app = new Microsoft.Office.Interop.Outlook.Application();
}
return app;
}
private void button1_Click(object sender, EventArgs e)
{
const string fileName = @"D:\MyDings.pst";
var app = GetOutlookApp();
var nameSpace = app.GetNamespace("MAPI");
nameSpace.AddStore(fileName);
MessageBox.Show("Done");
}
Adding a folder to a pst file from outlook in c#
You need to use Store.GetRootFolder()
to get a handle to the root folder of that store (not Store.Session
). So you would use:
// create a folder or subfolder in pst
Outlook.MAPIFolder pstRootFolder = NewPst.GetRootFolder();
Outlook.MAPIFolder NewFolder = pstRootFolder.Folders.Add("New Test Folder");
I recommend bookmarking both of the following: The PIA documentation isn't always complete, so it's worth checking out the COM documentation as well for complete class and member info.
- (.NET): Outlook 2007 Primary Interop Assembly Reference
- (COM): Outlook Object Model Reference
How can I create an Outlook PST file using .Net?
I was able to piece this code together from a variety of samples around the internet and MSDN docs. This will allow you to choose an outlook high level folder and will backup all folders underneath. In my case I didn't actually want mail folders so I exclude them.
Const BACKUP_PST_PATH As String = "C:\backup.pst"
Dim oFolder As Outlook.MAPIFolder = Nothing
Dim oMailbox As Outlook.MAPIFolder = Nothing
Dim app As New Outlook.Application()
Dim ns As Outlook.NameSpace = app.GetNamespace("MAPI")
Try
//if the file doesn not exist, outlook will create it
ns.AddStore(BACKUP_PST_PATH)
oFolder = ns.Session.Folders.GetLast()
oMailbox = ns.PickFolder()
For Each f As Outlook.Folder In oMailbox.Folders
If f.DefaultItemType <> Microsoft.Office.Interop.Outlook.OlItemType.olMailItem And f.FolderPath <> oFolder.FolderPath Then
f.CopyTo(oFolder )
End If
Next
ns.RemoveStore(oFolder)
Catch ex As Exception
ns.RemoveStore(oFolder)
IO.File.Delete(BACKUP_PST_PATH)
Throw ex
End Try
Reading an Outlook 2003 OST file
If you want to do it as an exercise you may want to write your own library.
- PST format specification
- Wiki article on PST and OST file formats
- lipff - opensource C library and tools to access the Personal Folder File (PFF) and the Offline Folder File (OFF) format. PFF is used in PAB (Personal Address Book), PST (Personal Storage Table) and OST (Offline Storage Table) files.
- PST file format SDK C++. Can read PST and OST files.
Related Topics
For I = 0, Why Is (I += I++) Equal to 0
.Net Timezoneinfo from Olson Time Zone
Run Code Once Before and After All Tests in Xunit.Net
Using JSON.Net to Return Actionresult
How Expensive Is the Lock Statement
How to Disable Copy, Paste and Delete Features on a Textbox Using C#
ASP.NET Core 3.0 System.Text.JSON Camel Case Serialization
Watermark in System.Windows.Forms.Textbox
How to Write One to Many Query in Dapper.Net
Passing an Enum Value as Command Parameter from Xaml
How to Make Method Call Another One in Classes
Enable Options Header for Cors on .Net Core Web API
Using Variables Inside Strings