C# MS Exchange Move Email To Folder

Solved!

The Move command failed regardless of several attempts because the ID was malformed. Apparently a move operation doesn't allow use of names. I had tried DisplayName as an identifier and that's what kept throwing me off. Finally, I gave up on DisplayName, which would have helped. Instead I pointed to the ID (which stopped the annoying "ID is malformed" error) by storing it in a variable, and the move worked.

Code:

Folder rootfolder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot);
rootfolder.Load();

foreach (Folder folder in rootfolder.FindFolders(new FolderView(100)))
{
    // Finds the emails in a certain folder, in this case the Junk Email
    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.JunkEmail, new ItemView(10));

    // This IF limits what folder the program will seek
    if (folder.DisplayName == "Example")
    {
        // Trust me, the ID is a pain if you want to manually copy and paste it. This stores it in a variable
        var fid = folder.Id;
        Console.WriteLine(fid);
        foreach (Item item in findResults.Items)
        {
            // Load the email, move the email into the id.  Note that MOVE needs a valid ID, which is why storing the ID in a variable works easily.
            item.Load();
            item.Move(fid);
        }
    }
}

It seems you are using EWS Managed API so here is my answer how I do such things.

Move method on items can accept WellKnownFolderName or folder id. If I understand it correctly you want to move you email into folder named "Example". So first you need to get folder object for this folder:

var filter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Example");
var view = new FolderView(1)
{
    PropertySet = new PropertySet(BasePropertySet.FirstClassProperties)
};
var findFoldersResults = exService.FindFolders(filter, view);
folder = findFoldersResults.FirstOrDefault(f => f.DisplayName.Equals("Example", StringComparison.OrdinalIgnoreCase));

Now you should have your "Example" folder variable and you can pass its id to Move method of an email. For more details check msdn pages about how to work with EWS Managed API, quite a lot of simple and basic usage examples there.

BTW: WellKnownFolderNames enum is a convenience type for most common Exchange folders like Inbox, Sent Items, etc. Anything else you have to retrieve on your own by searching an/or binding just in case of any other Exchange objects.


Based on these answers, created a working method for moving to folders, might be useful to someone:

/// <summary>
/// Moves the email to the specified folder.
/// </summary>
/// <param name="mail">Email message to move.</param>
/// <param name="folderName">Display name of the folder.</param>
public void MoveToFolder(EmailMessage mail, string folderName)
{
    Folder rootfolder = Folder.Bind(_exchangeService, WellKnownFolderName.MsgFolderRoot);
    rootfolder.Load();
    Folder foundFolder = rootfolder.FindFolders(new FolderView(100)).FirstOrDefault(x => x.DisplayName == folderName);
    if (foundFolder == default(Folder))
    {
        throw new DirectoryNotFoundException(string.Format("Could not find folder {0}.", folderName));
    }

    mail.Move(foundFolder.Id);
}