2017-02-13 18 views
2

我有一些代碼將項目從Deleted Items移動到郵件文件夾中。該代碼運行良好,並通常移動所有項目。當它遇到不是IPM的項目時會發生此問題。它給出了一個空引用的錯誤(請參閱:What is a NullReferenceException, and how do I fix it?EWS FindItemsResults <Item> Item.Move()不會將某些項目類型移動到Mail文件夾,如IPM.Appointment

這是奇怪的,因爲那裏有項目,它不能爲空。

下面是一個代碼摘錄:

// Specify the Exchange Service 
ExchangeService E_SERVICE = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 

// Look at the root of the Mailbox (Top of Information Store) 
FolderId fldr_id = WellKnownFolderName.MsgFolderRoot; 

// Define the folder view 
FolderView newFV = new FolderView(1000); 

// Perform a deep traversal 
newFV.Traversal = FolderTraversal.Deep; 

// Get the results of all the folders in the Mailbox 
FindFoldersResults f_results = E_SERVICE.FindFolders(fldr_id, newFV); 

// Define the source and target folder id variables as null. 
FolderId src_fldr_id = null; 
FolderId tgt_fldr_id = null; 

// Define the folders we are looking to move items from the source to the target 
string source = "Deleted Items" 
string target = "Old Deleted Items" 

// Search through all the folders found in the mailbox 
foreach (Folder fldr in f_results) 
{ 
    // If the source folder name is the same as the current folder name then set the source folder ID 
    if (fldr.DisplayName.ToLower() == source.ToLower()) 
    { 
     src_fldr_id = fldr.Id; 
    } 
    // If the target folder name is the same as the current folder name then set the target folder ID 
    if (fldr.DisplayName.ToLower() == target.ToLower()) 
    { 
     tgt_fldr_id = fldr.Id; 
    } 
} 

// Get all the items in the folder 
FindItemsResults<Item> findResults = E_SERVICE.FindItems(src_fldr_id, new ItemView(1000)); 

// If the number of results does not equal 0 
if (findResults.TotalCount != 0) 
{ 
    // For each item in the folder move it to the target folder located earlier by ID. 
    foreach(Item f_it in findResults) 
    { 
     f_it.Move(tgt_fldr_id); 
    } 
} 

我們得到扔在以下行錯誤:

 f_it.Move(tgt_fldr_id); 

這是一個空引用異常,可並非如此,因爲是那裏的項目,它通常是一個不是IPM.Note的項目。

那麼我該如何解決這個問題,並確保物品被移動,而不管它是什麼類型?

我以前在這裏發佈了關於這個Unable to move Mail items of different Item classes from the same folder using EWS

只有被擊落它是一個NullReferenceException時,這是不是這樣的!

所以任何有用的答案將不勝感激。

+0

'文件夾src_folder = Folder.Bind(E_SERVICE,src_fldr_id);'你做這個綁定,但沒有在其他地方使用它? –

+0

@CallumLinington抱歉被意外複製了。我現在已經修改過了。 –

回答

0

好的解決方案來解決這個問題是要確保你的load()項目執行移動()

確保您使用try..catch塊和處理異常象下面前:

try 
{ 
    f_it.Move(tgt_fldr_id); 
} 
catch (Exception e) 
{ 
    Item draft = Item.Bind(E_SERVICE, f_it.Id); 
    draft.Load(); 
    draft.Move(tgt_fldr_id); 
} 

這將強制項目單獨加載,然後移動它,即使它確實會拋出錯誤。爲什麼,這是不是已知的呢?但應該有希望幫助那些努力與你爲什麼你得到一個NullReferenceException

謝謝大家!

編輯:您可能需要閱讀爲什麼某些項目是空,因爲這將幫助你處理空項返回一個好一點https://social.msdn.microsoft.com/Forums/exchange/en-US/b09766cc-9d30-42aa-9cd3-5cf75e3ceb93/ews-managed-api-msgsender-is-null?forum=exchangesvrdevelopment

相關問題