簡體   English   中英

在C#中訪問Outlook 2010郵件文件夾

[英]Accessing Outlook 2010 Mail Folder in C#

我正在開發一個Outlook加載項,最近為了熟悉而切換到C#(我是一個Java人)。 此時,我只是嘗試遍歷郵件文件夾並將每條消息的主題打印到控制台,主要是為了確保一切正常工作到目前為止。 但是,無論何時運行它,我都會收到以下錯誤:

無法完成操作。 一個或多個參數值無效。

例外文字:

System.ArgumentException:無法完成操作。 一個或多個參數值無效。 位於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()

加載的裝配:

我對此感到有些困惑,因為這是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;
        }    

您應該調試或添加trace語句以查看pickFolder.EntryIDpickFolder.StoreID的值。 沒有有效的EntryIDStoreID ,它將拋出此錯誤。

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

如果用戶單擊取消按鈕,您應該檢查pickFolder是否為null。

此外,如果您讓用戶選擇一個文件夾,則無需再通過GetFolderFromID選擇該文件夾 - 您已經有了對它的引用。

當然,如果你想要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;

目前尚不清楚你是否通過@SilverNinja指出調試器中的代碼。 確保StoreID和EntryID有效非常重要。

還有一些可能性:

您的Outlook PST略有損壞。 嘗試scanPST,看看是否有幫助。

另外,你會認為pickFolder枚舉足夠智能,跳過這些,但你在文件夾樹的頂層還有其他項目而不是文件夾嗎? 我在枚舉聯系人和在我的聯系人文件夾中包含非聯系人項目時遇到了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM