18

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.

huseyint
  • 14,953
  • 15
  • 56
  • 78

7 Answers7

10

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

Paul Batum
  • 8,165
  • 5
  • 40
  • 45
  • 1
    it's not bad (its free & open source!) but doesnt support any of the date fields on a msg (yet) – Steve Casey Jun 07 '10 at 23:13
  • With a slight modification this can save attachments as well (right now it just displays whether there are any, and the size). – James Skemp Mar 24 '12 at 13:37
  • @Steve one commenter shared the instructions for getting the sent / received date here: http://www.codeproject.com/Articles/32899/Reading-an-Outlook-MSG-File-in-C?msg=2932319#xx2932319xx (scroll to the end) – Ciaran Jul 03 '13 at 13:33
  • @Ciaran wow, blast from the past. thankfully since then, i've never needed to go anywhere near outlook ;) – Steve Casey Jul 10 '13 at 23:38
9

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
  • 14,953
  • 15
  • 56
  • 78
8

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
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
Knox
  • 2,909
  • 11
  • 37
  • 65
6

Microsoft has documented this: .MSG File Format Specification

Shog9
  • 156,901
  • 35
  • 231
  • 235
Robby Slaughter
  • 1,530
  • 1
  • 11
  • 9
3

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
  • 156,901
  • 35
  • 231
  • 235
1

You can try our (commercial) Rebex Secure Mail library. It can read Outlooks MSG format. Following code shows how:

// Load message
MailMessage message = new MailMessage();
message.Load(@"c:\Temp\t\message.msg");

// show From, To and Sent date
Console.WriteLine("From: {0}", message.From);
Console.WriteLine("To: {0}", message.To);
Console.WriteLine("Sent: {0}", message.Date.LocalTime);

// find and try to parse the first 'Received' header
MailDateTime receivedDate = null;
string received = message.Headers.GetRaw("Received");
if (received != null)
{
    int lastSemicolon = received.LastIndexOf(';');
    if (lastSemicolon >= 0)
    {
        string rawDate = received.Substring(lastSemicolon + 1);
        MimeHeader header = new MimeHeader("Date", rawDate);
        receivedDate = header.Value as MailDateTime;
    }
}

// display the received date if available
if (receivedDate != null)
    Console.WriteLine("Received: {0}", receivedDate.LocalTime);

More info on Sent and Received dates and how are they represented in the message can be found at http://forum.rebex.net/questions/816/extract-senttime-receivetime-and-time-zones

Martin Vobr
  • 5,757
  • 2
  • 37
  • 43
0

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
  • 15,460
  • 3
  • 35
  • 34