C# EWS FindItemsResults<;项目>;Item.Move()不会将某些项目类型移动到邮件文件夹,例如IPM.Appointment
我有一些代码可以将邮件从已删除的邮件移动到邮件文件夹中。代码运行良好,通常会移动所有项。当遇到不是IPM的项时,会出现问题。注意。它给出的错误为空引用(请参阅:) 这很奇怪,因为那里有项目,它不能为空 以下是代码摘录:C# EWS FindItemsResults<;项目>;Item.Move()不会将某些项目类型移动到邮件文件夹,例如IPM.Appointment,c#,email,exchange-server,exchangewebservices,C#,Email,Exchange Server,Exchangewebservices,我有一些代码可以将邮件从已删除的邮件移动到邮件文件夹中。代码运行良好,通常会移动所有项。当遇到不是IPM的项时,会出现问题。注意。它给出的错误为空引用(请参阅:) 这很奇怪,因为那里有项目,它不能为空 以下是代码摘录: // Specify the Exchange Service ExchangeService E_SERVICE = new ExchangeService(ExchangeVersion.Exchange2010_SP2); // Look at the root of t
// 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);
}
}
这是一个空引用异常,不可能是这种情况,因为存在项目,并且通常不是IPM.Note
那么,我该如何着手解决这个问题,并确保项目得到移动,而不管它是什么类型的
我以前在这里发布过关于这个的信息
只有当情况并非如此时,才会被击落为NullReferenceException
因此,任何有用的答案都将不胜感激。好的,解决这个问题的方法是确保在执行移动()之前加载()项目 确保使用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而苦苦挣扎的人
谢谢大家
编辑:您可能想了解某些项目为空的原因,因为这将帮助您更好地处理返回的空项目。
文件夹src\u Folder=Folder.Bind(E\u服务,src\u fldr\u id)代码>您执行此绑定,但不在其他任何地方使用它?@Callumlington sorry是意外复制过来的。我现在已作出修订。
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);
}