views:

1792

answers:

7

I need to read from Outlook .MSG file in .NET without using COM API for Outlook (cos it will not be installed on the machines that my app will run). Are there any free 3rd party libraries to do that? I want to extract From, To, CC and BCC fields. Sent/Receive date fields would be good if they are also stored in MSG files.

A: 

If you open the .MSG file in a text editor, i believe you will find that the information you are after is stored as plain text inside the file. (It is on all the messages i have checked at least)

It would be pretty easy to write some code to parse the file looking for lines beginning with "From:" or "To:" etc. and then extracting the information you need.

If you need the body of the email as well, that may be a bit more complicated.

Jarod Elliott
I believe thats .eml files .msg are binary
tigermain
They are, but he is correct. Try it and see for yourself
rotard
+4  A: 

Microsoft has documented this: .MSG File Format Specification

Robby Slaughter
+3  A: 

Update: I have found a 3rd party COM library called Outlook Redemption which is working fine for me at the moment. If you use it via COM-Interop in .NET, don't forget to release every COM object after you are done with it, otherwise your application crashes randomly.

huseyint
wow, I just gave this a try and it's super easy to use. I'll add an answer with a test harness bit of code.
Knox
+1  A: 

It's a "Structured Storage" document. I've successfully used Andrew Peace's code to read these in the past, even under .NET (using C++/CLI) - it's clean and fairly easy to understand. Basically, you need to figure out which records you need, and query for those - it gets a little bit hairy, since different versions of Outlook and different types of messages will result in different records...

Shog9
+2  A: 

There is code avaliable on CodeProject for reading .msg files without COM. See here.

Paul Batum
Steve
+2  A: 

Here's some sample VBA code using Outlook Redemption that Huseyint found.

Public Sub ProcessMail()

   Dim Sess As RDOSession
   Dim myMsg As RDOMail
   Dim myString As String

   Set Sess = CreateObject("Redemption.RDOSession")

   Set myMsg = Sess.GetMessageFromMsgFile("C:\TestHarness\kmail.msg")

   myString = myMsg.Body

   myMsg.Body = Replace(myString, "8750", "XXXX")

   myMsg.Save

End Sub
Knox
A: 

Try DCI PST Reader at http://dci-software.com/pstreader.html