Unable to move Mail items of different Item classes from the same folder using EWS

元气小坏坏 提交于 2019-12-11 06:22:40

问题


EDIT: The cause of this is now known to be because those items are not of the same ItemClass. So if you have a folder that has IPM.Note, IPM.Appointment, IPM.Contact along with messages you will get this problem.

So the question is how do you handle this scenario.

Original Post below:
I have a piece of code that moves items from one folder to another using the EWS API and C#.

This is not a duplicate to What is a NullReferenceException, and how do I fix it? as this issue seems to occur within the API.

The exception is being handled but the original cause of this is unknown as there are items that are not being moved.

It seems to work fine for most items in a folder barring a few where it throws the message:

"Object reference not set to an instance of an object."

The code essentially looks like this:

FindItemsResults<Item> findResults = E_SERVICE.FindItems(src_fldr_id, new ItemView(1000000));

if (findResults.TotalCount != 0)
{
    for (int i = 0; i < findResults.TotalCount; i++)
    {
        try
        {
            findResults.Items[i].Move(tgt_fldr_id);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }                        
    }
}

This actually works for the most part. I have looked at what items it is struggling to move and it appears to be Calendar Invites or Draft items.

Anyone know how to get around this problem and force those items to be moved rather than generating a null item.

Here is the full function as defined. Apologies in advance for the poor commenting. Still in a scratch stage.

    static private bool MoveItemsInternal(FolderId fldr_id,string source, string target)
    {
        FolderView newFV = new FolderView(100000);
        newFV.Traversal = FolderTraversal.Deep;
        FindFoldersResults f_results = E_SERVICE.FindFolders(fldr_id, newFV);
        FolderId src_fldr_id = null;
        FolderId tgt_fldr_id = null;
        StringList errors = new StringList();

        Console.WriteLine("Total number of Folders in Destination Mailbox " + TARGETMAILBOX + ": " + f_results.TotalCount.ToString());

        int src_count = 0;
        int tgt_count = 0;


        foreach (Folder fldr in f_results)
        {
            if (fldr.DisplayName.ToLower() == source.ToLower())
            {
                src_fldr_id = fldr.Id;
                src_count++;
            }

            if (fldr.DisplayName.ToLower() == target.ToLower())
            {
                tgt_fldr_id = fldr.Id;
                tgt_count++;
            }
        }

        if (src_fldr_id != null)
        {
            if (src_count < 2)
            {
                Console.WriteLine("Source Folder " + source + " found!");
            }
            else
            {
                Console.WriteLine("More than " + src_count.ToString() + " source Folders Found");
                return false;
            }
        }
        else
        {
            Console.WriteLine("Source Folder Not Found");
            return false;
        }

        if (tgt_fldr_id != null)
        {
            if (tgt_count < 2)
            {
                Console.WriteLine("Target Folder " + target + " found!");
            }
            else
            {
                Console.WriteLine("More than " + src_count.ToString() + " target Folders Found");
                return false;
            }
        }
        else
        {
            Console.WriteLine("Target Folder Not Found");
            return false;
        }

        if (src_count == 1 && tgt_count == 1)
        {
            Folder src_folder = Folder.Bind(E_SERVICE, src_fldr_id);

            FindItemsResults<Item> findResults = E_SERVICE.FindItems(src_fldr_id, new ItemView(1000000));

            if (findResults.TotalCount != 0)
            {
                Console.WriteLine("Total Mail Count :" + findResults.TotalCount);
                Console.WriteLine("Moving all mail items to new location");

                for (int i = 0; i < findResults.TotalCount; i++)
                {
                    try
                    {
                        Console.WriteLine(TARGETMAILBOX + "\\" + source + "\\" + findResults.Items[i].Subject.ToString());
                        findResults.Items[i].Move(tgt_fldr_id);
                    }
                    catch (Exception e)
                    {
                        errors.Add("Item " + i + " Error: " + e.Message);
                    }                        
                }
            }
            else
            {
                Console.WriteLine("No Mail Items found");
            }

            FindFoldersResults f_fldr_result = E_SERVICE.FindFolders(src_fldr_id, newFV);
            if(f_fldr_result.TotalCount!=0)
            {
                Console.WriteLine("\n\nMoving all Sub folders to new location\n");
                foreach (Folder folder in f_fldr_result)
                {
                    Console.WriteLine(TARGETMAILBOX + "\\" + source + "\\" + folder.DisplayName.ToString());
                    folder.Move(tgt_fldr_id);
                }
            }else
            {
                Console.WriteLine("No Folders found");
            }
        }

        if(errors.Count!=0)
        {
            Console.WriteLine("Errors have been encountered:\n\n");
            foreach(string s in errors)
            {
                Console.WriteLine(s);
            }
        }

        return true;
    }

来源:https://stackoverflow.com/questions/42161157/unable-to-move-mail-items-of-different-item-classes-from-the-same-folder-using-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!