简体   繁体   English

Outlook加载项无法将mailitem添加到自定义文件夹中

[英]Outlook add-in, cannot add mailitem into a custom folder

I want to take mails from inbox and sentbox folders, compare their subjects and if they match, put it all into a new custom folder. 我想从收件箱和发件箱文件夹中收集邮件,比较他们的主题,如果匹配,则将其全部放入新的自定义文件夹中。 Here's the code so far: 这是迄今为止的代码:

Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)
        this.Application.ActiveExplorer().Session.GetDefaultFolder
        (Outlook.OlDefaultFolders.olFolderInbox);

// I also have made this for the sentBox folder //我也为sentBox文件夹做了这个

            string userName = (string)this.Application.ActiveExplorer()
                .Session.CurrentUser.Name;
            Outlook.MAPIFolder customFolder = null;

                customFolder = (Outlook.MAPIFolder)inBox.Folders.Add(userName,
                    Outlook.OlDefaultFolders.olFolderInbox);

                inBox.Folders[userName].Display();

// This is the custom folder in which i wish to place the matching mails //这是我希望放置匹配邮件的自定义文件夹

        for (int i = 1; i <= sentboxFolder.Items.Count; i++)
        {
            outboxItem =  sentboxFolder.Items[i];


            for (int a = 1; a <= inBox.Items.Count; a++)
            {
               inboxItem = inBox.Items[a];

                if ("RE: " + outboxItem.Subject == inboxItem.Subject)
                {

                    customFolder.Items.Add(inboxItem);

// Here I loop through the inbox and outbox folders and if the subjects match I want to add the inbox part to the custom folder. //这里我遍历收件箱和发件箱文件夹,如果主题匹配,我想将收件箱部分添加到自定义文件夹。

I have 3 questions: 1. Is there a way to put both matching mails into one folder ? 我有3个问题:1。有没有办法将两个匹配的邮件放入一个文件夹? 2. I know there should be a smarter way to this aside from comparing subjects, Can anyone help how to use conversation ID here? 2.我知道除了比较主题之外,应该有一个更聪明的方法,任何人都可以帮助如何在这里使用对话ID吗? 3. I get an exception at the last line, that it cannot add inbox item into custom folder because it's not an actual object instance. 3.我在最后一行遇到异常,它无法将收件箱项添加到自定义文件夹中,因为它不是实际的对象实例。 Where should I instantiate mailitem to fix this ? 我应该在哪里实例化mailitem来解决这个问题?

Thanks in advance. 提前致谢。

Firstly, do not use multiple dot notation, especially in a loop - cache the Items collection before entering the loop. 首先,不要使用多点符号,特别是在循环中 - 在进入循环之前缓存Items集合。

Secondly, do not just loop through all items in a folder looking for a match - use Items.Find. 其次,不要只遍历查找匹配项的文件夹中的所有项目 - 使用Items.Find。

That being said, you can use MailItem.Move(OtherFolder) . 话虽这么说,你可以使用MailItem.Move(OtherFolder)。 If you want to preserve the original item, use MailItem.Copy (returns new item), then move it to the target folder. 如果要保留原始项目,请使用MailItem.Copy(返回新项目),然后将其移动到目标文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM