2013-11-04 27 views
2

我想使用C#來訪問我的Outlook發送文件夾,並將郵件移動到我的PST中稱爲存檔的文件夾中。這是我正在使用的代碼,但我收到了多個編譯錯誤。在這裏有更多的編碼經驗的人知道如何做到這一點?從評論C#將郵件移動到PST

static void MoveMe() 
{ 
try 
{ 
    _app = new Microsoft.Office.Interop.Outlook.Application(); 
    _ns = _app.GetNamespace("MAPI"); 
    _ns.Logon(null, null, false, false); 
    Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox); 
    Outlook.Items SentMailItems = SentMail.Items; 
    Outlook.MailItem newEmail = null; 
    foreach (object collectionItem in SentMailItems) 
    { 
     moveMail.Move(Archive); 
    } 
} 
catch (System.Runtime.InteropServices.COMException ex) 
{ 
    Console.WriteLine(ex.ToString()); 
} 
finally 
{ 
    _ns = null; 
    _app = null; 
    _inboxFolder = null; 
} 
} 

錯誤列表:

  1. Only assignment, call, increment, decrement, and new object expressions can be used as a statement
  2. The type or namespace name 'Emails' could not be found (are you missing a using directive or an assembly reference?)
  3. The name 'A_Sent' does not exist in the current context
  4. The name 'moveMail' does not exist in the current context
  5. The name 'SentMail' does not exist in the current context
+0

你的錯誤是什麼? –

+0

1)只能使用賦值,調用,增量,減量和新的對象表達式作爲語句\t 2)他找不到類型或命名空間名稱'電子郵件'(您是否缺少using指令或程序集引用? ) 3)名稱'A_Sent'在當前上下文中不存在 4)名稱'moveMail'在當前上下文中不存在 5)名稱'SentMail'在當前上下文中不存在 – MasterOfStupidQuestions

+2

我不得不將其標記爲脫離主題,因爲「詢問代碼的問題必須顯示對所解決問題的最小理解」,因爲您擁有的一些問題非常基本(即,「moveMail」的定義在哪裏,「SentMail」定義等)並且與您的實際問題無關。 – admdrew

回答

6

這裏是一個如何獲取源文件夾(SentItems)並將它們移動到PST(存檔)的示例。

using Outlook = Microsoft.Office.Interop.Outlook; 

public void MoveMyEmails() 
    { 
     //set up variables 
     Outlook.Application oApp = null; 
     Outlook.MAPIFolder oSource = null; 
     Outlook.MAPIFolder oTarget = null; 
     try 
     { 
      //instantiate variables 
      oApp = new Outlook.Application(); 
      oSource = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail); 
      oTarget = oApp.Session.Folders["Archive"]; 
      //loop through the folders items 
      for (int i = oSource.Items.Count; i > 0; i--) 
      { 
       move the item 
       oSource.Items[i].Move(oTarget); 
      } 
     } 
     catch (Exception e) 
     { 
      //handle exception 
     } 
     //release objects 
     if (oTarget != null) 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(oTarget); 
      GC.WaitForPendingFinalizers(); 
      GC.Collect(); 
     } 
     if (oSource != null) 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(oSource); 
      GC.WaitForPendingFinalizers(); 
      GC.Collect(); 
     } 
     if (oApp != null) 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp); 
      GC.WaitForPendingFinalizers(); 
      GC.Collect(); 
     } 

    } 
+0

我真的需要停止幫助Rep 1點的人,浪費我的時間! – Sorceri

+0

我試着按照你的代碼,它給我的COMExeption在 'oTarget = oApp.Session.Folders [「Archive」] ;'用_object找不到_ 我知道這是一個很老的帖子,但你能幫我一下嗎? – Aferrercrafter

+1

@Aferrercrafter使用PST文件夾的實際名稱替換Archive。 – Sorceri

相關問題