C# MS Exchange Move Email To Folder

坚强是说给别人听的谎言 提交于 2019-12-10 12:57:27

问题


Added: thanks to user @grapkulec, I am using

using Microsoft.Exchange.WebServices.Data;

I am trying to move an email to a folder that I've already created in Outlook (using MS Exchange). So far, I've been able to move the email to the drafts or another well known folder name, but have had no success moving it to a folder I created called "Example."

foreach (Item email in findResults.Items)
email.Move(WellKnownFolderName.Drafts);

The above code works; but I don't want to use the well known folders. And, if I try to change the code to something like:

email.Move(Folder.(Example));

or

email.Move(Folder.["Example"]);

It doesn't move (in both cases, throws an error). I've found tons of examples of how to move emails into folders on MSDN, SO and general C# - but ONLY of folders that are "well known" to Outlook (Drafts, Junk Email, etc), which doesn't work with a folder that I've created.


回答1:


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);
        }
    }
}



回答2:


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.




回答3:


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);
}


来源:https://stackoverflow.com/questions/13685782/c-sharp-ms-exchange-move-email-to-folder

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