2009-08-12 68 views
6

我需要从Outlook消息文件中读取内容。目前,我使用CodeProject.com项目的类来完成此任务,因为在服务器上部署VSTO和Outlook不是一种选择。如何从Outlook MSG文件中读取收到的日期 - 不使用Outlook API?

除了日期信息(例如收到日期和发送日期)之外,此类还可以从msg文件中获取To,From,CC,Subject,Body和其他所有我需要的内容。

关于如何从msg文件中删除MSDN文件,有一些(非常低级的)documentation,但它略微超出了本项目的范围,并且根本没有提及日期。

理想情况下,我可以为我现在使用的类(前面提到的CodeProject中的OutlookStorage.cs)提供一个插入式替换,或者可以修改现有的类。要修改,我需要接收日期的正确的4个字符的十六进制道具标识符。例如,Subject被列为PR_SUBJECT = "0037",Body被列为PR_BOY = "1000"

回答

2

我认为Aspose库会做你想要的,确定它是第三方库,所以可能不是你想要的。有几个vbs脚本可以从可以翻译的msg文件中获取基本信息。

1

得到了暗示从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; 
    } 
} 

这是怎样的一个黑客相比,你可以使用的东西这个lovely project得到MSG文件其他事情的方式。尽管如此,它还是能够抵挡我对它所投的一切,正如前面提到的那样,愚弄它的方法就是以正确的格式在主题行中输入精确到秒的日期。

1

您的两个职位合并,我建议以下解决方案:

要修改,我需要正确的4个字符的十六进制支撑标识符收到日期。例如,Subject被列为PR_SUBJECT =“0037”,Body被列为PR_BOY =“1000”。

寻找“007D”。

在接收到的数据中使用您在第二篇文章中发布的方法来消除在主题内存在相同(日期)字符串时的问题。


我不得不提一下,这种方法似乎并没有对内部电子邮件的工作:在我从同事那里收到邮件,也没有substg1.0_007Dxxxx属性。

这里,日期似乎隐藏在substg1.0_0047xxxx中。

一切顺利!

INNO

7

如果您使用OutlookStorage.cs从CodeProject,然后添加以下:

private const string PR_RECEIVED_DATE="007D"; 
private const string PR_RECEIVED_DATE_2 = "0047"; 

... 

/// <summary> 
/// Gets the date the message was received. 
/// </summary> 
public DateTime ReceivedDate 
{ 
    get 
    { 
     if (_dateRevieved == DateTime.MinValue) 
     { 
      string dateMess = this.GetMapiPropertyString(OutlookStorage.PR_RECEIVED_DATE); 
      if (String.IsNullOrEmpty(dateMess)) 
      { 
       dateMess = this.GetMapiPropertyString(OutlookStorage.PR_RECEIVED_DATE_2); 
      } 
      _dateRevieved = ExtractDate(dateMess); 
     } 
     return _dateRevieved; 
     //return ExtractDate(dateMess); 
    } 
} 

private DateTime _dateRevieved = DateTime.MinValue; 

private DateTime ExtractDate(string dateMess) 
{ 
    string matchStr = "Date:"; 

    string[] lines = dateMess.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); 
    foreach (string line in lines) 
    { 
     if (line.StartsWith(matchStr)) 
     { 
      string dateStr = line.Substring(matchStr.Length); 
      DateTime response; 
      if (DateTime.TryParse(dateStr, out response)) 
      { 
       return response; 
      } 
     } 
    } 
    return DateTime.MinValue;     
} 
相关问题