Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with reading *.msg outlook file using Microsoft.Office.Interop.Outlook

Tags:

c#

outlook

msg

I'm writing piece of code which will handle extraction of information from email stored in *msg Outlook file. The idea how to do it I took from C# Outlook interop and OpenSharedItem for opening MSG files. But when calling method OpenSharedItem a get such error System.AccessViolationException. Anyone know what's the problem?
Here is code causing error

Outlook._Application app = new Outlook.Application();
Outlook.NameSpace NS = app.GetNamespace("MAPI");
Outlook.MAPIFolder inboxFld = NS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.MailItem fld =  (Outlook.MailItem) app.Session.OpenSharedItem("E://Projects//C#//message1.msg");
like image 750
pchot Avatar asked Feb 24 '23 10:02

pchot


1 Answers

Ok. In case anybody will face the same problem finally I've got an answer. First of all should be

Outlook.MailItem fld =  (Outlook.MailItem) app.Session.OpenSharedItem("E:\\Projects\\C#\\message1.msg");

difference is in the slashes.
Then all you need to make it work is Outlook 2007 or newer. And that's it :]

like image 51
pchot Avatar answered May 02 '23 00:05

pchot