c# - 在 C# 中访问 Outlook 2010 邮件文件夹

标签 c# outlook add-in

我正在开发 Outlook 加载项,最近为了熟悉而切换到 C#(我本质上是一个 Java 人)。此时,我只是尝试遍历邮件文件夹并将每条消息的主题打印到控制台,主要是为了确保到目前为止一切正常。但是,每当我运行它时,都会收到以下错误:

Could not complete the operation. One or more parameter values are not valid.

异常文本:

System.ArgumentException: Could not complete the operation. One or more parameter values are not valid. at Microsoft.Office.Interop.Outlook.NameSpaceClass.GetFolderFromID(String EntryIDFolder, Object EntryIDStore) at OutlookAddIn2.ThisAddIn.ThisAddIn_Startup(Object sender, EventArgs e) at Microsoft.Office.Tools.AddInImpl.OnStartup() at Microsoft.Office.Tools.AddInImpl.AddInExtensionImpl.Microsoft.Office.Tools.EntryPoint.OnStartup() at Microsoft.Office.Tools.AddInBase.OnStartup() at OutlookAddIn2.ThisAddIn.FinishInitialization() at Microsoft.Office.Tools.AddInBase.Microsoft.Office.Tools.EntryPoint.FinishInitialization() at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase(ExecutionPhases executionPhases) at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.ExecuteEntryPoints()

加载的程序集:

我对此感到有些困惑,因为这是 Microsoft 在 MSDN 上推荐的让用户选择文件夹的精确方法。我已经包含了我的来源,如果您有任何想法,请告诉我。感谢您花时间阅读这篇文章,并愿意提供帮助!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace OutlookAddIn2
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //Get application namespace and grab the original folder object
            Outlook.Folder pickFolder = (Outlook.Folder)Application.Session.PickFolder();

            //Outlook.Folder mrFolder = Application.Session.GetFolderFromID(pickFolder.EntryID, pickFolder.StoreID) as Outlook.Folder;

            foreach (Outlook.MailItem oMailItem in pickFolder.Items)
            {
                Console.WriteLine(oMailItem.Subject);
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

最佳答案

托盘这个:

 public static Folder FOLDER_1;
 public static Folder FOLDER_2;
 public static Folder FOLDER_N;

/// <summary>
        /// Hilo que lee el archivo de datos PST del OUTLOOK
       private static void readPst()
        {
            try
            {
                Application app = new Application();
                NameSpace outlookNs = app.GetNamespace("MAPI");
                MAPIFolder mf = outlookNs.GetDefaultFolder(OlDefaultFolders.olFolderTasks);


                string names = mf.FolderPath.Split('\\')[2];



                Folder fMails = getFolder(fCarpetasPersonales.Folders, "Inbox");



                FOLDER_1= getFolder(fMails.Folders, "FOLDER_1");
                FOLDER_2= getFolder(fMails.Folders, "FOLDER_2");
                FOLDER_N= getFolder(fMails.Folders, "FOLDER_n");

//TO DO... For example:  foreach (object item in fMails.Items)



     private static Folder getFolder(Folders folders, string folder)
        {
            foreach (object item in folders)
            {
                if (item is Folder)
                {
                    Folder f = (Folder)item;
                    if (f.Name.Equals(folder))
                    {
                        return f;
                    }
                }
            }
            return null;
        }    

关于c# - 在 C# 中访问 Outlook 2010 邮件文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11416633/

相关文章:

c# - 如何传递按钮的命令参数

javascript - 添加查询字符串时验证不起作用

c# - 如何为具有不同名称的元素编写有效的 xsd

java - Outlook在java中为jacob.jar定制文件夹的整数值

python - 如何在 Python 中将文件作为附件添加到 Outlook 项目

ms-word - word插件弹出对话框

c# - Outlook 的 MailItem 正文中的按钮将调用内部插件函数

C#打印像素值

css - 多个 css 类不起作用

visual-studio-2010 - 在哪里可以找到Visual Studio Express的加载项?