Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中枚举Outlook项目时发生异常#_C#_Outlook_Limit_Rpc_Office Interop - Fatal编程技术网

C# 在C中枚举Outlook项目时发生异常#

C# 在C中枚举Outlook项目时发生异常#,c#,outlook,limit,rpc,office-interop,C#,Outlook,Limit,Rpc,Office Interop,我正在尝试编写一个应用程序,它将监视几个邮箱,当发现邮件时,从每个项目中获取一些信息,然后一旦我有了项目列表,我就可以采取适当的措施 但无论我如何处理它,我都达到了Exchange强制的255 RPC连接限制 我完全无法确定是什么原因导致了错误-据我所知,我已将所有内容都绑定到一个方法中,并正在调用Marshal.ReleaseComObject。。。。我甚至接受了打开和关闭Outlook应用程序句柄本身的性能影响 任何建议都将不胜感激。。。(我似乎不明白为什么我的代码在预览中看起来是错误的,所

我正在尝试编写一个应用程序,它将监视几个邮箱,当发现邮件时,从每个项目中获取一些信息,然后一旦我有了项目列表,我就可以采取适当的措施

但无论我如何处理它,我都达到了Exchange强制的255 RPC连接限制

我完全无法确定是什么原因导致了错误-据我所知,我已将所有内容都绑定到一个方法中,并正在调用Marshal.ReleaseComObject。。。。我甚至接受了打开和关闭Outlook应用程序句柄本身的性能影响

任何建议都将不胜感激。。。(我似乎不明白为什么我的代码在预览中看起来是错误的,所以为了安全起见,我也把它放在了pastebin上…)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Office.Interop.Outlook;
使用Microsoft.Office.Interop;
使用System.Runtime.InteropServices;
命名空间HandleMailingResponses
{
类OutlookFolderTableScraper
{
公用列表GetItemsFromFolder(字符串folderName)
{
List returnList=新列表();
Application outlookHandle=新应用程序();
NameSpace-outlookNamespace=outlookHandle.GetNamespace(“MAPI”);
Folders rootOutlookFolders=outlookNamespace.Folders;
Logon(null,null,null,true);
Folder requestedRoot=枚举文件夹(rootOutlookFolders,folderName);
Folders theseFolders=requestedRoot.Folders;
文件夹thisInbox=枚举文件夹(这些文件夹,“收件箱”);
Marshal.ReleaseComObject(requestedRoot);
requestedRoot=null;
Marshal.ReleaseComObject(rootOutlookFolders);
rootOutlookFolders=null;
字符串storeID=thisInbox.storeID;
Table thisTable=thisInbox.GetTable(“,OlTableContents.olUserItems);
//默认情况下,每个项目都有EntryID、Subject、CreationTime、LastModificationTime和MessageClass列
//我们可以添加MailItem或ReportItem对象将具有的任何其他属性。。。。
Columns theseColumns=此表。Columns;
添加(“发送邮件地址”);
Marshal.ReleaseComObject(此收件箱);
thisInbox=null;
outlookNamespace.Logoff();
Marshal.ReleaseComObject(outlookNamespace);
outlookNamespace=null;
outlookHandle.Quit();
Marshal.ReleaseComObject(了望手柄);
outlookHandle=null;
整数计数=0;
而(!thisTable.EndOfTable)
{
Row thisRow=thisTable.GetNextRow();
对象[]theseValues=(对象[])thisRow.GetValues();
WriteLine(“已处理{0}”,count++);
//从该项获取主体
字符串messageClass=(字符串)这些值[4];
字符串entryID=(字符串)这些值[0];
string body=getItemBody(entryID、storeID、messageClass);
添加(新的OutlookItem((string)theseValues[5],(string)theseValues[1],body,messageClass,entryID));
}
退货清单;
}
私有字符串getItemBody(字符串entryID、字符串storeID、字符串messageClass)
{
Application outlookHandle=新应用程序();
NameSpace-outlookNamespace=outlookHandle.GetNamespace(“MAPI”);
Logon(null,null,null,true);
弦体;
if(messageClass.ToLower().StartsWith(“报告”))
{
ReportItem thisItem=(ReportItem)outlookNamespace.GetItemFromID(entryID,storeID);
body=此项。body;
此项。关闭(OlInspectorClose.olDiscard);
//释放此com引用
释放结果;
做
{
releaseResult=Marshal.ReleaseComObject(此项);
}while(releaseResult!=0);
}
其他的
{
MailItem thisItem=(MailItem)outlookNamespace.GetItemFromID(entryID,storeID);
body=此项。body;
此项。关闭(OlInspectorClose.olDiscard);
//释放此com引用
释放结果;
做
{
releaseResult=Marshal.ReleaseComObject(此项);
}while(releaseResult!=0);
}
outlookNamespace.Logoff();
outlookNamespace=null;
outlookHandle.Quit();
outlookHandle=null;
GC.Collect();
GC.WaitForPendingFinalizers();
返回体;
}
/// 
///遍历Outlook.Folders对象,搜索具有给定名称的文件夹
/// 
///Outlook.Folder对象
/// 
/// 
私有文件夹枚举文件夹(文件夹根文件夹、字符串目标文件夹)
{
文件夹returnFolder=null;
System.Collections.IEnumerator thisEnumerator=rootFolders.GetEnumerator();
while(thisEnumerator.MoveNext())
{
文件夹f=(文件夹)thisEnumerator.Current;
字符串名称=f.名称;
if(targetFolder.ToLower().Equals(name.ToLower()))
{
returnFolder=f;
打破
}
}
ICustomAdapter=(ICustomAdapter)此枚举器;
Marshal.ReleaseComObject(adapter.getUnderlineObject());
适配器=空;
返回文件夹;
}
}
}

不要每次调用函数时都创建一个新的应用程序对象,将其作为类的私有成员变量保留到y
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop;
using System.Runtime.InteropServices;

namespace HandleMailingResponses
{
    class OutlookFolderTableScraper
    {
        public List<OutlookItem> GetItemsFromFolder(string folderName)
        {
            List<OutlookItem> returnList = new List<OutlookItem>();

            Application outlookHandle = new Application();
            NameSpace outlookNamespace = outlookHandle.GetNamespace("MAPI");
            Folders rootOutlookFolders = outlookNamespace.Folders;

            outlookNamespace.Logon(null, null, null, true);

            Folder requestedRoot = enumerateFolders(rootOutlookFolders, folderName);
            Folders theseFolders = requestedRoot.Folders;
            Folder thisInbox = enumerateFolders(theseFolders, "Inbox");

            Marshal.ReleaseComObject(requestedRoot);
            requestedRoot = null;
            Marshal.ReleaseComObject(rootOutlookFolders);
            rootOutlookFolders = null;

            string storeID = thisInbox.StoreID;

            Table thisTable = thisInbox.GetTable("",OlTableContents.olUserItems);

            //By default each item has the columns EntryID, Subject, CreationTime, LastModificationTime and MessageClass
            //we can add any of the other properties the MailItem or ReportItem object would have....
            Columns theseColumns = thisTable.Columns;
            theseColumns.Add("SenderEmailAddress");

            Marshal.ReleaseComObject(thisInbox);
            thisInbox = null;

            outlookNamespace.Logoff();
            Marshal.ReleaseComObject(outlookNamespace);
            outlookNamespace = null;
            outlookHandle.Quit();
            Marshal.ReleaseComObject(outlookHandle);
            outlookHandle = null;

            int count = 0;
            while (!thisTable.EndOfTable)
            {
                Row thisRow = thisTable.GetNextRow();
                object[] theseValues = (object[]) thisRow.GetValues();
                Console.WriteLine("processed {0}",count++);

                //get the body from this item
                string messageClass = (string)theseValues[4];
                string entryID = (string)theseValues[0];
                string body = getItemBody(entryID,storeID, messageClass);

                returnList.Add(new OutlookItem((string)theseValues[5], (string)theseValues[1], body, messageClass, entryID));
            }



            return returnList;
        }

        private string getItemBody(string entryID, string storeID, string messageClass)
        {
            Application outlookHandle = new Application();
            NameSpace outlookNamespace = outlookHandle.GetNamespace("MAPI");
            outlookNamespace.Logon(null, null, null, true);
            string body;

            if (messageClass.ToLower().StartsWith("report"))
            {
                ReportItem thisItem = (ReportItem)outlookNamespace.GetItemFromID(entryID, storeID);
                body = thisItem.Body;
                thisItem.Close(OlInspectorClose.olDiscard);
                //release this com reference
                int releaseResult;
                do
                {
                    releaseResult = Marshal.ReleaseComObject(thisItem);
                } while (releaseResult != 0);
            }
            else
            {
                MailItem thisItem = (MailItem)outlookNamespace.GetItemFromID(entryID, storeID);
                body = thisItem.Body;
                thisItem.Close(OlInspectorClose.olDiscard);
                //release this com reference
                int releaseResult;
                do
                {
                    releaseResult = Marshal.ReleaseComObject(thisItem);
                } while (releaseResult != 0);
            }

            outlookNamespace.Logoff();
            outlookNamespace = null;
            outlookHandle.Quit();
            outlookHandle = null;


            GC.Collect();
            GC.WaitForPendingFinalizers();

            return body;
        }

                    /// <summary>
        /// Iterates through an Outlook.Folders object searching for a folder with the given name
        /// </summary>
        /// <param name="rootFolder">An Outlook.Folder object</param>
        /// <param name="targetFolder"></param>
        /// <returns></returns>
        private Folder enumerateFolders(Folders rootFolders, string targetFolder)
        {
            Folder returnFolder = null;
            System.Collections.IEnumerator thisEnumerator = rootFolders.GetEnumerator();
            while (thisEnumerator.MoveNext())
            {
                Folder f = (Folder)thisEnumerator.Current;
                string name = f.Name;
                if (targetFolder.ToLower().Equals(name.ToLower()))
                {
                    returnFolder = f;
                    break;
                }
            }
            ICustomAdapter adapter = (ICustomAdapter)thisEnumerator;
            Marshal.ReleaseComObject(adapter.GetUnderlyingObject());
            adapter = null;
            return returnFolder;
        }
        }

}