2009-08-12 11 views
6

Ho bisogno di leggere cose da un file di Outlook. Attualmente sto usando una classe dal progetto CodeProject.com per realizzare questo, poiché la distribuzione di VSTO e Outlook su un server non è un'opzione.Come si legge la data di ricezione dai file MSG di Outlook, senza l'API di Outlook?

Questa classe ottiene A, Da, CC, Oggetto, Corpo e qualsiasi altra cosa di cui ho bisogno dal file msg, ad eccezione delle informazioni sulla data (come la data di ricezione e la data di invio).

C'è uno (davvero, veramente basso livello) documentation su come ottenere informazioni dai file msg su MSDN, ma è un po 'oltre lo scopo di questo progetto e non menziona affatto le date.

Idealmente, sarei in grado di avere una sostituzione drop-in per la classe che sto usando ora (OutlookStorage.cs nel CodeProject precedentemente citato) o essere in grado di modificare un po 'la classe esistente. Per modificare, avrei bisogno del corretto identificatore esadecimale di 4 caratteri per la data di ricezione. Ad esempio, Oggetto è elencato come PR_SUBJECT = "0037" e Corpo è elencato come PR_BOY = "1000".

risposta

2

Penso che la libreria Aspose farà quello che vuoi, ok è una lib di terze parti quindi potrebbe non essere quello che vuoi. Ci sono alcuni script vbs in giro che ricevono informazioni di base dai file msg che potrebbero essere tradotti.

1

Got un suggerimento da 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; 
    } 
} 

Si tratta di una specie di hack rispetto ai modi per ottenere altre cose da file MSG utilizzando qualcosa di questo lovely project. Tuttavia, si è opposto a tutto ciò che ho lanciato contro di esso e, come notato, l'unico modo per ingannare è mettere la data esatta al secondo nella riga dell'oggetto nel formato corretto.

1

per combinare le due post vorrei suggerire la seguente soluzione:

Per modificare, avrei bisogno del corretto 4 caratteri esadecimali prop identificatore per la data ricevuto. Ad esempio, l'oggetto è elencato come PR_SUBJECT = "0037" e il corpo è elencato come PR_BOY = "1000".

Cercare "007D".

Utilizzare il metodo inserito nel secondo messaggio sui dati ricevuti per eliminare il problema quando la stessa stringa (data) si trova all'interno dell'oggetto.


Devo dire che questo metodo non sembra di lavorare su e-mail interne: in mail che ricevo da parte di colleghi, non v'è alcuna substg1.0_007Dxxxx-proprietà.

Qui, la data sembra essere nascosta in substg1.0_0047xxxx.

Tutto il meglio!

inno

7

Se stai usando OutlookStorage.cs da CodeProject, aggiungere il seguente:

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;     
} 
Problemi correlati