2013-05-04 38 views
4

我已經看到了pst和ost文件之間的差異,目前正在通過下面給出的以下代碼訪問outlook pst文件。 有什麼辦法可以使用相同的代碼來訪問ost文件嗎?有人可以提到我嗎?訪問Outlook ost文件

private DataTable GetInboxItems() 
{ 
    DataTable inboxTable; 
    //try 
    //{ 
    filter = "[ReceivedTime] >= '" + dtpStartDate.Value.ToString("dd/MM/yyyy 12:00 AM") + "' and [ReceivedTime] <= '" + dtpEndDate.Value.ToString("dd/MM/yyyy 11:59 PM") + "'"; 
    Outlook.Application outlookApp = GetApplicationObject(); 
    Outlook.Folder root = outlookApp.Session.DefaultStore.GetRootFolder() as Outlook.Folder; 
    EnumerateFolders(root); 
    //string filter = "[ReceivedTime] > '" + dtpStartDate.Value.ToString("dd/MM/yyyy") + "'"; 

    //inbox 
    Outlook.MAPIFolder inboxFolder = outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); 
    inboxTable = CreateTable(); 
    int count = 0; 


    if (inboxFolder.Items.Count > 0) 
    { 

     var restrictedItems = inboxFolder.Items.Restrict(filter); 
     restrictedItems.Sort("[ReceivedTime]", true); //descending 
     //foreach (var item in inboxFolder.Items) 
     foreach (var item in restrictedItems) 
     { 
      var mail = item as Outlook.MailItem; 
      if (mail != null) 
      { 
       //try 
       //{ 
       DataRow row = inboxTable.NewRow(); 
       //row["sn"] = (++count).ToString(); 
       row["sn"] = mail.EntryID + " " + mail.ReceivedByEntryID; 
       row["MailType"] = "Inbox"; 
       row["SenderName"] = mail.SenderName; 
       row["SenderEmail"] = mail.SenderEmailAddress; 
       row["ReceivedDate"] = mail.ReceivedTime; 
       row["Subject"] = mail.Subject; 
       row["Body"] = mail.Body != null ? (mail.Body.Length > 25 ? mail.Body.Substring(0, 25) : mail.Body) : null; 
       //row["Body"] = mail.Body != null ? mail.Body : ""; 
       row["MailSize"] = mail.Size.ToString(); 
       string attachments = null; 
       if (mail.Attachments.Count > 0) 
       { 
        foreach (var attachment in mail.Attachments) 
        { 
         if (((Outlook.Attachment)attachment) != null) 
          //attachments = ((Outlook.Attachment)attachment).FileName + " " + ((Outlook.Attachment)attachment).Size.ToString() + ", "; 
          attachments += (((Outlook.Attachment)attachment).Size/1024).ToString() + " KB, "; 
        } 
       } 


       row["AttachmentCount"] = mail.Attachments.Count; 
       if (attachments != null) 
        row["AttachmentSize"] = attachments.Substring(0, attachments.Length - 2); 

       inboxTable.Rows.Add(row); 
      } 
      //catch (Exception ex) 
      //{ 

      // return null; 
      //} 

     } 
    } 

    return inboxTable; 
} 
+1

該代碼可以使用存儲對象,它不關心,如果商店是PST,OST或不離線在線交流文件。這段代碼有問題嗎?也許它需要憑據... – Arthur 2013-05-05 07:30:27

+0

謝謝亞瑟...我如何訪問OST文件?你能否請示例代碼證明什麼時候需要證書?這個OST文件提供的PST文件有什麼好處? – newbie 2013-05-06 12:17:45

+0

OST在Exchange帳戶類型中使用緩存模式時創建,您無法控制它。 PST用於存儲郵件數據,無需服務器帳戶或使用其他類型的帳戶(如IMAP)。如果默認商店屬於Exchange帳戶,則您問題中的代碼將讀取OST文件。你試過了嗎?你有問題嗎? – Arthur 2013-05-07 11:12:59

回答

1

你需要教育自己什麼OST/PST是因爲對它們的訪問是沒有太大的區別,都是Store對象,所以它們具有相同的接口。

試試這個來源的開始和實驗自己,因爲它是瞭解東西如何工作的最佳方式。

http://en.wikipedia.org/wiki/Personal_Storage_Table

http://msdn.microsoft.com/en-us/library/bb609139(v=office.14).aspx

http://msdn.microsoft.com/en-us/library/ff184648(v=office.14).aspx

http://msdn.microsoft.com/en-us/library/bb208208(v=office.12).aspx

http://www.c-sharpcorner.com/UploadFile/rambab/OutlookIntegration10282006032802AM/OutlookIntegration.aspx

+0

***如何使用C#管理'OST'文件以轉換爲_PST_或只是從** OST **中讀取電子郵件? – Kiquenet 2016-10-21 22:38:48

4

後,我想通了,如何真正建立由OP提供的代碼,我發現得到它非常有用在Outlook上開始,所以我w我想分享下面的完整代碼。

using System; 
using System.Collections.Generic;//List 
using System.Linq;//Array 
//using System.Text; 
//using System.Threading.Tasks; 
using System.Diagnostics;//Process 
using System.Runtime.InteropServices;//Marshal 
using System.Data;//DataTable 
using System.Reflection;//Missing 
using Microsoft.Office.Interop.Outlook;//.OST files, needs Microsoft.Office.Interop.Outlook.dll 

//TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line. 
using Outlook = Microsoft.Office.Interop.Outlook; 

namespace WatchOutlookMails 
{ 
    class StoreFormat 
    { 
     public DataTable GetInboxItems(DateTime dtpStartDate, DateTime dtpEndDate) 
     { 
      DataTable inboxTable; 
      string filter = string.Format("[ReceivedTime] >= '{0:dd/MM/yyyy 12:00 AM}' and [ReceivedTime] <= '{1:dd/MM/yyyy 11:59 PM}'", dtpStartDate, dtpEndDate); 
      Outlook.Application outlookApp = GetApplicationObject(); 
#if false//only needed if you want to select another folder 
      Outlook.Folder root = outlookApp.Session.DefaultStore.GetRootFolder() as Outlook.Folder; 
      EnumerateFolders(root); 
#endif 
      //inbox 
      Outlook.MAPIFolder inboxFolder = outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); 
      inboxTable = CreateTable(); 
      if (inboxFolder.Items.Count > 0) 
      { 
       Items restrictedItems = inboxFolder.Items.Restrict(filter); 
       const bool SortDescending = true; 
       restrictedItems.Sort("[ReceivedTime]", SortDescending); 
       foreach (var item in restrictedItems)//item is a "COM Object" (?) 
       { 
        MailItem mail = item as Outlook.MailItem; 
        if (mail != null) 
        { 
         //try 
         //{ 
         DataRow row = inboxTable.NewRow(); 
         //row["sn"] = (++count).ToString(); 
         row["sn"] = mail.EntryID + " " + mail.ReceivedByEntryID; 
         row["MailType"] = "Inbox"; 
         row["SenderName"] = mail.SenderName; 
         row["SenderEmail"] = mail.SenderEmailAddress; 
         row["ReceivedDate"] = mail.ReceivedTime; 
         row["Subject"] = mail.Subject; 
         row["Body"] = mail.Body != null ? (mail.Body.Length > 25 ? mail.Body.Substring(0, 25) : mail.Body) : null; 
         //row["Body"] = mail.Body != null ? mail.Body : ""; 
         row["MailSize"] = mail.Size.ToString(); 
         int AttachmentSize = 0; 
         foreach (Outlook.Attachment attachment in mail.Attachments) 
         { 
          if (attachment != null) 
           AttachmentSize += attachment.Size; 
         } 
         row["AttachmentCount"] = mail.Attachments.Count; 
         row["AttachmentSize"] = AttachmentSize; 
         inboxTable.Rows.Add(row); 
         //catch (Exception ex) 
         //{ 
         // break; 
         //} 
        } 
       } 
      } 
      return inboxTable; 
     } 

     private DataTable CreateTable() 
     { 
      DataTable T = new DataTable(); 
      T.Columns.Add("sn", typeof(string)); 
      T.Columns.Add("MailType", typeof(string)); 
      T.Columns.Add("SenderName", typeof(string)); 
      T.Columns.Add("SenderEmail", typeof(string)); 
      T.Columns.Add("ReceivedDate", typeof(string)); 
      T.Columns.Add("Subject", typeof(string)); 
      T.Columns.Add("Body", typeof(string)); 
      T.Columns.Add("MailSize", typeof(int)); 
      T.Columns.Add("AttachmentCount", typeof(int)); 
      T.Columns.Add("AttachmentSize", typeof(int)); 
      return T; 
     } 

     private void EnumerateFoldersInDefaultStore() 
     { 
      Outlook.Application outlookApp = GetApplicationObject(); 
      Outlook.Folder root = outlookApp.Session.DefaultStore.GetRootFolder() as Outlook.Folder; 
      EnumerateFolders(root); 
      return; 
     } 

     private void EnumerateFolders(Outlook.Folder folder) 
     { 
      Outlook.Folders childFolders = folder.Folders; 
      if (childFolders.Count > 0) 
      { 
       foreach (Outlook.Folder childFolder in childFolders) 
       { 
        // Write the folder path. 
        //Debug.WriteLine(childFolder.FolderPath); 
        // Call EnumerateFolders using childFolder. 
        // Uses recursion to enumerate Outlook subfolders. 
        EnumerateFolders(childFolder); 
       } 
      } 
      return; 
     } 

     /// <summary> 
     /// obtain an Application object that represents an active instance of Microsoft Outlook, 
     /// if there is one running on the local computer, or to create a new instance of Outlook, 
     /// log on to the default profile, and return that instance of Outlook 
     /// </summary> 
     /// <returns></returns> 
     private Outlook.Application GetApplicationObject() 
     { 
      // source: https://msdn.microsoft.com/en-us/library/ff462097.aspx 
      Outlook.Application application = null; 

      // Check whether there is an Outlook process running. 
      if (Process.GetProcessesByName("OUTLOOK").Count() > 0) 
      { 

       // If so, use the GetActiveObject method to obtain the process and cast it to an Application object. 
       application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; 
      } 
      else 
      { 

       // If not, create a new instance of Outlook and log on to the default profile. 
       application = new Outlook.Application(); 
       Outlook.NameSpace nameSpace = application.GetNamespace("MAPI"); 
       nameSpace.Logon("", "", Missing.Value, Missing.Value); 
       nameSpace = null; 
      } 

      // Return the Outlook Application object. 
      return application; 
     } 
    }//end class 
}//end ns 

爲了填補缺失的部分,我從https://support.microsoft.com/en-us/kb/310259借來的,用於EnumerateFolders:https://msdn.microsoft.com/en-us/library/office/ff184607.aspx