views:

719

answers:

3

I need to read stuff from an Outlook msg file. Currently I'm using a class from CodeProject.com project to accomplish this, since deploying VSTO and Outlook on a server is not an option.

This class gets To, From, CC, Subject, Body, and everything else I need from the msg file, except Date information (such as Recieved Date and Sent Date).

There is some (really, really low-level) documentation on how to get stuff out of msg files on MSDN, but it's a little beyond the scope of this project and doesn't mention dates at all.

Ideally I'd be able to have a drop-in replacement for the class I am using now (OutlookStorage.cs in the previously mentioned CodeProject) or be able to modify the existing class a bit. To modify, I would need the correct 4 character hexidecimal prop identifier for recieved date. For instance, Subject is listed as PR_SUBJECT = "0037" and Body is listed as PR_BOY = "1000".

A: 

Got a hint from this:

string fullFileName = "c:\message.msg";
DateTime dateRevieved = new DateTime();

StreamReader sr = new StreamReader(fullFileName, Encoding.Default);
string full = sr.ReadToEnd();

string date;
int iStart;
int iLast;

string caption;

//This -should- handle all manner of screwage
//The ONLY way it would not is if someone guessed the -exact- to-the-second
//time that they send the message, put it in their subject in the right format
while (true) {      //not an infinite loop, I swear!

    caption = "Date:";
    if (full.IndexOf("Date:") > -1) {   //full shortens with each date is removed
     string temp = "";

     iStart = full.LastIndexOf(caption);
     temp = full.Remove(0, iStart + caption.Length);
     full = full.Substring(0, iStart);

     iLast = temp.IndexOf("\r\n");
     if (iLast < 0) {
      date = temp;
     } else {
      date = temp.Substring(0, iLast);
     }

     date = date.Trim();

     if (date.Contains(subject) || subject.Contains(date)) {
      continue;   //would only happen if someone is trying to screw me
     }

     try {
      dateRevieved = DateTime.Parse(date); //will fail if not a date
      break;   //if not a date breaks out of while loop
     } catch {
      continue;   //try with a smaller subset of the msg
     }
    } else {
     break;
    }
}

This is kind of a hack compared to the ways you can get other things from msg files using something this lovely project. Still, it's stood up to everything I have thrown against it, and as noted the -only- way to fool it is to put the exact to-the-second date in the subject line in the proper format.

matthews
+1  A: 

I think the Aspose library will do what you want, ok it a 3rd party lib so may not be what you want. There are a few vbs scripts around that get basic infomation out of msg files that could be translated.

76mel
A: 

Hi matthews,

to combine your two posts I would suggest the following solution:

To modify, I would need the correct 4 character hexidecimal prop identifier for recieved date. For instance, Subject is listed as PR_SUBJECT = "0037" and Body is listed as PR_BOY = "1000".

Look for "007D".

Use the method you posted in your second post on the received data to eliminate the problem when the same (date) string is inside the subject.


I have to mention that this method doesn't seem to work on internal eMails: In mails I receive from colleagues, there is no substg1.0_007Dxxxx-Property.

Here, the date seems to be hidden in substg1.0_0047xxxx.

All the best!

inno

Inno