如何以编程方式将任务添加到特定的Outlook存储

时间:2019-03-31 17:14:10

标签: c# outlook

我有一段代码可以获取特定商店的任务。

Outlook.Application app = new Outlook.Application();
        Outlook.NameSpace ns = app.Session;
        Outlook.Stores stores = ns.Stores;

        try
        {
            foreach (Outlook.Store store in stores)
            {
                if (store.DisplayName == comboBoxAccounts.Text)
                {
                    Outlook.MAPIFolder tasksFolder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);

                    foreach (Outlook.TaskItem task in tasksFolder.Items)
                    {
                        if (task.Subject == comboBoxContacts.Text)
                        {
                            comboBoxProperties.Items.Add(task.Body);
                        }
                    }

                    if (tasksFolder != null)
                        Marshal.ReleaseComObject(tasksFolder);
                }
            }
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            statusLabel.Text = ex.ToString();
        }
        finally
        {
            if (ns != null)
                Marshal.ReleaseComObject(ns);
        }

我正在尝试将等价物放在一起,以将任务添加到特定商店。下面的代码将任务添加到Outlook中的主要帐户,但我想使用另一个帐户。

Outlook.Application app = new Outlook.Application();
            Outlook.NameSpace ns = app.Session;

            try
            {
                Outlook.TaskItem task = app.CreateItem(Outlook.OlItemType.olTaskItem);
                task.Subject = strContact;
                task.StartDate = DateTime.Now;
                task.DueDate = DateTime.Now.AddDays(2);
                task.Status = Outlook.OlTaskStatus.olTaskNotStarted;
                task.Body = xDoc.ToString();
                task.Save();

            }
            finally
            {

            }

我该如何执行类似于store.CreateItem()的操作?

谢谢

更新: 解决方案

Outlook.Application app = new Outlook.Application();
            Outlook.NameSpace ns = app.Session;
            Outlook.Stores stores = ns.Stores;

            try
            {
                foreach (Outlook.Store store in stores)
                {
                    if (store.DisplayName == strAccount)
                    {
                        Outlook.MAPIFolder tasksFolder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
                        Outlook.TaskItem task = (Outlook.TaskItem)tasksFolder.Items.Add(Outlook.OlItemType.olTaskItem);

                        task.Subject = strContact;
                        task.StartDate = DateTime.Now;
                        task.DueDate = DateTime.Now.AddDays(2);
                        task.Status = Outlook.OlTaskStatus.olTaskNotStarted;
                        task.Body = xDoc.ToString();

                        task.Save();
                    }
                }
            }
            finally
            {

            }

1 个答案:

答案 0 :(得分:1)

请勿使用Application.CreateItem。从Application.Session.Stores集合中检索商店,调用Store.GetDefaultFolder(olFolderTasks),然后使用MAPIFolder.Items.Add在该文件夹中添加商品。