简体   繁体   English

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

[英]Accessing Outlook 2010 Mail Folder in C#

I am working on an Outlook Add-In, and recently switched to C# for sake of familiarity (I am a Java man at heart). 我正在开发一个Outlook加载项,最近为了熟悉而切换到C#(我是一个Java人)。 At this point, I am simply trying to iterate through a mail folder and print the subject of each message to the console, mainly as a way to make sure everything is working alright so far. 此时,我只是尝试遍历邮件文件夹并将每条消息的主题打印到控制台,主要是为了确保一切正常工作到目前为止。 Whenever I run it, however, I receive the following error: 但是,无论何时运行它,我都会收到以下错误:

Could not complete the operation. 无法完成操作。 One or more parameter values are not valid. 一个或多个参数值无效。

Exception Text: 例外文字:

System.ArgumentException: Could not complete the operation. System.ArgumentException:无法完成操作。 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.Office.Tools的Microsoft.Office.Tools.AddInImpl.OnStartup()的OutlookAddIn2.ThisAddIn.ThisAddIn_Startup(Object sender,EventArgs e)中的Microsoft.Office.Interop.Outlook.NameSpaceClass.GetFolderFromID(String EntryIDFolder,Object EntryIDStore) Microsoft.Office.Tools.AddInBase.Microsoft.Office.Tools中的OutlookAddIn2.ThisAddIn.FinishInitialization()中的Microsoft.Office.Tools.AddInBase.OnStartup()中的.AddInImpl.AddInExtensionImpl.Microsoft.Office.Tools.EntryPoint.OnStartup() Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime中的Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase(ExecutionPhases executionPhases)中的.EntryPoint.FinishInitialization()。 .Interop.IExecuteCustomization2.ExecuteEntryPoints()

Loaded Assemblies: 加载的装配:

I am somewhat baffled by this, as this is the precise method recommended by Microsoft on MSDN to have a user select a folder. 我对此感到有些困惑,因为这是Microsoft在MSDN上推荐的用户选择文件夹的精确方法。 I have included my source, please let me know if you have any thoughts. 我已经包含了我的来源,如果您有任何想法,请告诉我。 Thanks for taking the time to read this post, and for being willing to help out! 感谢您花时间阅读这篇文章,并愿意提供帮助!

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

Tray this: 托盘:

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

You should debug or add trace statements to see the values of pickFolder.EntryID and pickFolder.StoreID . 您应该调试或添加trace语句以查看pickFolder.EntryIDpickFolder.StoreID的值。 Without a valid EntryID or StoreID it will throw this error. 没有有效的EntryIDStoreID ,它将抛出此错误。

Trace.TraceInformation("EntryID: {0}\tStoreID: {1}", pickFolder.EntryID, pickFolder.StoreID);

You should check if pickFolder is null should the user click the cancel button. 如果用户单击取消按钮,您应该检查pickFolder是否为null。

Also, if you are letting the user pick a folder, you don't need to select the folder again via GetFolderFromID - you already have a reference to it. 此外,如果您让用户选择一个文件夹,则无需再通过GetFolderFromID选择该文件夹 - 您已经有了对它的引用。

Can you try this, of course, if you want Inbox folder: 当然,如果你想要Inbox文件夹,你可以尝试这个吗?

Outlook.Application app = new Outlook.Application();

Outlook.NameSpace ns = app.GetNamespace("MAPI");

Outlook.Folder folder = app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;

It's not clear whether you stepped through the code in the debugger as @SilverNinja pointed out. 目前尚不清楚你是否通过@SilverNinja指出调试器中的代码。 Ensuring that StoreID and EntryID are valid is important. 确保StoreID和EntryID有效非常重要。

There are another few possibilities: 还有一些可能性:

Your Outlook PST is slightly corrupt. 您的Outlook PST略有损坏。 Try scanPST and see if that helps. 尝试scanPST,看看是否有帮助。

Also, you would think that the pickFolder enumeration would be smart enough, to skip these, but do you have other items at the top level of folder tree than folders? 另外,你会认为pickFolder枚举足够智能,跳过这些,但你在文件夹树的顶层还有其他项目而不是文件夹吗? I've actually had this problem with enumerating contacts and having non-contact items in my contacts folder. 我在枚举联系人和在我的联系人文件夹中包含非联系人项目时遇到了这个问题。

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

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