Home » C# Programming

How to parse .msg file without using Outlook object model in .NET?

How to parse .msg file without using Outlook object model in .NET?

I want to extract From, To, Subject and Body from tjhe .msg file.

Any pointer will be helpful.



4 Answers Found


Answer 1


Read the following article which describes how to read an Outlook msg file in C# without the Outlook object model.

Reading an Outlook MSG File in C#

Also take a look at the following links.

Parse Outlook .msg files without using Outlook

Parse .msg file using C#


Answer 2


See if below steps works for you.


See if you can  Convert MSG file to txt file (http://us.generation-nt.com/best-way-convert-msg-file-txt-help-22848812.html). Convert txt file into XML format. Use XmlTextReader to read the file content Convert the XML file into your own format using XSLT transformation

Code which can be used for XSLT transformation:

Code I am using for XSLT Transformation:

XslCompiledTransform xslt;

String xsltFileName =”mainTrans.xslt”;

XPAthDocument xDoc ; // reading using XPathNavigation

XsltSettings settings = new XsltSettings(true, false);

XmlUrlResolver resourceResolver = new XmlUrlResolver();

resourceResolver = new ResourceResolver(Assembly.GetExecutingAssembly(), “Prefixdllname”);

XPathDocument xslDoc = new XPathDocument(Assembly.GetExecutingAssembly().GetManifestResourceStream(prefix + "." + xsltFileName));

xslt.Load(xslDoc, settings, resourceResolver); // Loading Embedded XSLT resources into  XslCompiledTransform

// Main document part processing

xslt.Transform(xDoc, args, intermidaryOutput); // Debugging at this point


You can use XmlReader instead of XPathDocument.


For code sample on codeplex refer link. http://www.codeproject.com/KB/office/MsgReader.aspx

Not sure whether you are allowed to use third party/free source code.

Also Check whether below thread will give you result.


Nasir Khan

Answer 3

Go through below link: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/fdb9117d-f507-431b-8f29-8d5d823de665

Answer 4

I've had a lot of success using MSG .NET to parse 1000s of .msg files.

It is commercial library http://www.independentsoft.de/msg/index.html




I'm trying to create an msg file (just simple stuff inside, basic html or and image) but this should run without Outlook installed. So, no MAPI automation.

Any pointers or ideas ?

Thanks in advance.



i want to access querystring using client object model using ECMA script.

for eg http://localhost:80/demopage.aspx?test=123

can we read value of test using ECMA script





i have several saved files .MSG emails in my PC folders under the  Outlook 2007(Windows XP)

i have Form Region an Outlook 2007

Error:  1.  open Outlook 2007

2. open  file.msg -

3. close file.msg

4. open file.msg - error cant open file,  file in use

if  not open outlook2007 all OK! open file.msg, close, open


We have developed a protocol handler for an email store which is very similar

to that of Outlook’s MAPI protocol handler. Our email store can have emails

as well as their attachments indexed.

In Vista, we have problems in getting the preview pane working for Outlook

(.msg) files. The preview handlers for all other standard types (.doc, .xls, .ppt,

.txt etc.) like are working fine. We just want to use an existing preview

handler for .msg files.

So, what we done is a two step process that has been mentioned on MSDN

and various forums.

1.In IShellFOlder::GetUIObject() we handle IID_IQueryAssociations as

CComPtr<IQueryAssociations> spqa;
AssocCreate(CLSID_QueryAssociations, __uuidof(IQueryAssociations), &spqa);

// For Word files, we pass ".doc", for text files ".txt" etc.

// Here we are passing the value for .msg files

spqa->Init(0, L"MAPI/IPM.Message", NULL, NULL);

spqa->QueryInterface(riid, ppvReturn);

2.In IShellFolder::BindToObject() we return IStream of our file (.doc, .xls, .ppt, .txt etc)

Step 1 and 2 above works fine for all other file types (except .msg) for which there is an existing preview handler available,

which is essentially the key value of {8895b1c6-b41f-4c1c-a562-0d564250836f},

available under the HKCR\<File Ext>\ShellEx\{8895b1c6-b41f-4c1c-a562-0d564250836f}.

For a .msg file the type is a MAPI/IPM.Message, whose preview handler is available,

as there is a key value for it under the key


But as mentioned in point 1 while handling IID_IQueryAssociations we have also tried

passing “.msg” and “Outlook.File.msg” instead of “MAPI/IPM.Message

but IShellFOlder::BindToObject is never called for an IStream.

Using the Task Manager we have observed that prevhost.exe (a process for hosting

preview handlers) does get loaded with the DLL mssvp.dll.

Are we using the right preview handler for .msg files?


I'm working on a process where I have a bunch .msg files on a file system that I'm absorbing into a memory through a migration application.  I'm wanting to then import these messages into Outlook itself or a pst file for archiving to an email repository.  The problem that I'm running into is that I need to import these messages as is (so I want to keep everything about the message the same body, send date time, received data time etc.).  I need to accomplish this using MAPI but I have yet to find a way to import these existing messages.  All I've been able to do is create a new message item (MailItem) set its attributes and then save to a folder.



I am building an Outlook addin using c# VS2010. I have outlook 2010. In my add in, I move emails from outlook to a database or network folder and back again. I have built custom panes in my code and can show all emails taken out of outlook in a treeview on the custom pane with the list of emails shown in another custom pane. However , this is not what I really wanted to do. I want to show the data in an explorer pane so when the user clicks on a node in my customised pane, I can show all the details of emails stored in the database. Can this be done? I know that all explorer windows have an associated outlook folder, but in this case there isn't one. I just want to dislay the emails I have moved to my add in.



I am writing .msg files from VC++ using StgCreateDocfile routines, using the MS-OXMSG as a guideline.

The .msg files can be opened in Quick View Plus and other viewers, but Outlook will not open them, saying "Cannot Start Microsoft Office Outlook.  Cannot Read Item" when I double-click on the .msg file.

I've been dumping the Storage's various contents, and can't figure out which particular parts Outlook does not like.

Comparing with a "good" .msg file, the "__properties_version1.0" items for unicode strings (type 0x001f), the "length" on good .msg files are 2 + the actual length of the stream.  I changed my .msg files to do the same, but they are still corrupt.

Does anyone have experience with writing .msg files?  (I can send the "corrupt" .msg file if anyone is willing to have a look!)
Any suggestions what to try?
I've been deleting sections from the .msg file to see if any particular section is bad.

One problem I just found is the need for PR_STORE_SUPPORT_MASK,
I happened to stumble upon these pages:  http://support.microsoft.com/kb/884671, http://msdn.microsoft.com/en-us/library/cc815750.aspx
This fixes some of the .msg files.

Are there any other required field values?  (The ANSI emails do not open in Outlook, but they open in most viewers)


Unlike MAPI, OOM appears to be non-transactional. I have added a button to the new message inspector ribbon (C++, outlook 2007). In the button click handler, I get the current OOM MailItem, set ItemEvents_10 advise sinks for "write" and "send" and call MailItem::Send. When the "write" event handler is called, I set the subject of the mailItem but cancel the event and return S_OK. See below. The cancel works but, unexpectedly, the changed subject persists. "Send" behaves the same. I would have thought the change would be reverted. Am I doing something wrong or is this expected? Anyone know how to make OOM transactional?

HRESULT MyClass::ItemEvents_10_Write(VARIANT_BOOL* cancel)
    m_MailItem->put_Subject(L"changed"); // m_MailItem is an Outlook::_MailItem
    *cancel = TRUE;
    return S_OK;



i'm developing a plugin which analysis every incoming email on the fly. Gathered information from the this process I need to display in a custom created column (over Outlook i can do that with the field chooser but only available fields).

The question is now how the heck I'm able to add this. I searched the web for the past 2 hours without to find even one single hint about it :-(

Currently I only target Office 2010.

thanks in advance for any hint / solution.



Hi All,

Would it be ok if I asked in here to weigh in on following question posted in office forums:







"All calls to the Outlook object model execute on Outlook’s main foreground thread. The only threading model supported by the Outlook object model is single-threaded apartment (STA). Calling the Outlook object model from a background thread is not supported."

But Since I am running a very big process (opening emails, downloading attachments, saving emails) out of the PST file, I need to make my UI resposive and report appropriate message to the user.

What kind of framework or pattern should I use to accomplish this.

Previously, I was running OUTLOOK objects in background thread which work normally most of the time and I was able to make my UI reposive as well.  But for the huge PSTs OUTLOOK hangs in and out and I could not rely on this.

I am looking for any kind of help or suggestion.

Thanks in advance,

Bibek Dawadi

I am working on sharepoint portal and I wish to add already existing members to a exchange distribution list. Is there a way out to do this using Outlook object model. If not then is there a way out to display a distribution list members window using outlook object model.

As part of a web site I allow users to store documents in SQL Server 2000 using an Image column on a table. After the file is saved the user can open the document through the browser. This works great for most file extensions such as ".XLS" or ".DOC" but fails when attempting to open ".MSG" files (saved outlook messages). XLS documents will open in Excel and DOC documents open in Word, but MSG documents open in Notepad and display what appears to be binary data. I'm trying to figure out if this is a SQL Server issue or some sort of ASP/ASP.NET webserver issue.

If I attempt to open an MSG file stored directly on the web server, the file opens correctly using the "Microsoft Outlook View Control", but when attempting to open the same file saved in SQL Server, it attempts to open in notepad.

Any ideas?

Is it possible to create an .MSG file server side using ASP.NET and then send the file to the client? My specific need is to create emails automatically using data from the database and then give the users the ability to review the email prior to sending it. As opposed to automatically sending the email. I'd use the mailto: hyperlink, but the body could be rather large and it would cause the page to bloat. Multiple records will be displayed on a page.
Hi all.  I have a question regarding OfficeOpen XML and the.Net platform.  I work with Excel via Automation using VB.  I want to understand how the new XML formats for Excel 2007 are going to affect my approach to developing solutions for Excel.  Typically I use Automation by referencing the Excel Object Model 12.0 in my VB projects.  This allows me to access the Excel Object Model and the project runs by opening Excel on the client side. 

As I move forward, I now know that I can work with the Office Open XML and Open Document Format (ODF) for all spreadsheets.  If I decide to move towards a web-based solftware solution (say ASP.Net) and focus on the new XML format (Office Open XML and ODF), does this imply that I will no longer use the Excle Object Model via Automation?  Does this mean that I have to learn a new Object model for Excel XML and ODF?  Assume further that I may want to dump the spreadsheets into a SQL/Oracle Server at some point and reassemble them.  How do these new formats aid me in doing so?

I cannot seem to find any information to answer these questions.



Hi all,

i have to make an enterprise project that need to query some sharepoint list for retrieve data.
i will deploy the app on the same machine that run sharepoint site i need to query into.

i have to know if there are some limitation or some things to keep in mind if i make a new asp.net application run on 3.5 or 4.0 framework and have to reference wss 3.0 dll (that i know are developed under 2.0 framework). can i have some trouble doing this?

excuse for the bad english,




I have an application (web service actually, written in c#, visual stuido 2010 and using the new sharepoint 2010 client object model to connect to a sharepoint 2010 server) which is generating a file (actually its a PDF SQL report which was generated programatically, but that is not important for this question) and then trying to upload the document into a sharepoint 2010 document library. 

I'm trying to use the client object model to upload the document.  Not only do i want to just upload the document, i also would like to attach some meta data to the document when i upload it.  Is there and easy way to do this?  In sharepoint 2007 using server object model i could do it via the line of code where supSub is a SPFolder object an ht is a hash table of propery / values for the meta data i'm applying to the document (i can give more details on the 2007 solution if required)

 SPFile file = supSub.Files.Add(fileName, renderedBytes, ht, true);

While the File (versus SPFile on the server) object and FileCollection object (accessed by the Files property as above) do exist in the 2010 client object model the method call for Add has the form of Add(FileCreationInformation).  FileCreationInformation only has properties of Content, Overwrite and URl, i don't see a place for metadata using this method.

Looking at another path i found an article which gives a nice example of uploading a document to as sharepoint 2010 library an entirely different way (http://msdn.microsoft.com/en-us/library/ee956524(office.14).aspx#SP2010ClientOMOpenXml_Creating) but makes no mention of metadata. This uses SaveBinaryDirect ie

using System;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.SharePoint.Client;
// The following directive avoids ambiguity between the// System.IO.File class and Microsoft.SharePoint.Client.File class.using ClientOM = Microsoft.SharePoint.Client;
class Program
 staticvoid Main(string[] args)
 ClientContext clientContext =
 new ClientContext("http://intranet.contoso.com");
 using (FileStream fileStream =
 new FileStream("NewDocument.docx", FileMode.Open))
 "/Shared Documents/NewDocument.docx", fileStream, true);

So is there a way to do this or do i have to make a second pass, find my document by its name and attach the meta data separately (a bit more of pain i'd rather do it on upload)



I was wondering if it is possible to save/download a file from a Sharepoint Document Library using the Client Object Model?

Essentially, i have written a ribbon button - its main functionality is to process and send any selected documents as a byte array to a remote WCF service (via EMCAScript).

Using the SP2010 COM, i am able retrieve the file objects, but i could not find any methods of turning the file into a byte array.

Any advice would be greatly appreciated.





i need to convert all files into searchable pdf's using sharepoint object model.

can anyone help me,

thanks in advance


Hi All,

I am trying to upload files programmatically into SP2010 using Client Object Model.The code I am using is


FileStream fileStream = newFileStream(sharedPath, FileMode.Open);


File.SaveBinaryDirect(ctx, targetUrl, fileStream, true);

This code works fine when i upload files of smaller size.When i try to upload files of 800MB or  1 GB i get the Operation time out exception.The stack trace is given below.

"at System.Net.HttpWebRequest.GetResponse()\r\n   at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()\r\n   at Microsoft.SharePoint.Client.File.SaveBinary(ClientContext context, String serverRelativeUrl, Stream stream, String etag, Boolean overwriteIfExists, SaveBinaryCheckMode checkMode)\r\n   at Microsoft.SharePoint.Client.File.SaveBinaryDirect(ClientContext context, String serverRelativeUrl, Stream stream, Boolean overwriteIfExists)\r\n  "

I browsed a lot on this subject and came across some suggestions like changing the settings in IIs,web.config.None of these methods are working for me.Kindly suggest what else can be done to get rid of this exception.





<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure