2009-08-12 120 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;     
} 
相關問題