2017-02-13 57 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

相关问题