C#MS Exchange将电子邮件移至文件夹 [英] C# MS Exchange Move Email To Folder

查看:130
本文介绍了C#MS Exchange将电子邮件移至文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已添加:感谢用户@grapkulec,我使用的是

Added: thanks to user @grapkulec, I am using

using Microsoft.Exchange.WebServices.Data;

我正在尝试将电子邮件移至我已经在Outlook中创建的文件夹(使用MS交换)。到目前为止,我已经能够将电子邮件移动到草稿或其他知名的文件夹名称,但是将其移动到我创建的名为 Example的文件夹并没有成功。

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

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

它不会移动(在两种情况下均会引发错误)。我发现了无数示例,这些示例说明了如何将电子邮件移动到MSDN,SO和常规C#上的文件夹中,但是Outlook中知名的文件夹(草稿,垃圾电子邮件等),

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.

推荐答案

已解决!

Move 命令失败,无论尝试几次,都是因为ID格式错误。显然,移动操作不允许使用名称。我曾尝试使用 DisplayName 作为标识符,但这一直让我失望。最后,我放弃了 DisplayName ,这会有所帮助。相反,我通过将ID存储在变量中来指出ID(它停止了烦人的 ID格式错误错误),并且此举有效。

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.

代码:

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

这篇关于C#MS Exchange将电子邮件移至文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆