EWS FindItemsResults<Item> Item.Move() 不会将某些项目类型移动到邮件文件夹(如 IPM)。约会



我有一些代码正在将项目从"已删除邮件"中移动到"邮件文件夹"中。该代码运行良好,并且总体上移动了所有项目。当遇到不是IPM.Note的项时,就会出现此问题。它给出的错误是null引用(请参阅:什么是NullReferenceException,以及如何修复它?)

这很奇怪,因为那里有项目,而且不能为空。

以下是代码摘录:

// 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.的项目

那么,我该如何绕过这一点,并确保无论是什么类型的物品都能被移动呢?

我以前在这里发布过关于这一点。无法使用EWS 从同一文件夹中移动不同邮件类别的邮件

但当情况并非如此时,却被抨击为NullReferenceException!

因此,任何有用的答案都将不胜感激。

好的,解决这个问题的方法是确保在执行Move()之前加载()项目

一定要试试。。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关于为什么某些项目是Null的,因为这将帮助您更好地处理返回的Null项目。

最新更新