1

下面的代码十次中有九次有效,但在某些情况下我会收到以下错误:您的服务器管理员限制了您可以同时打开的项目数量。尝试关闭您已打开的邮件或从您正在撰写的未发送邮件中删除附件和图像。

我检查了它试图提取的电子邮件,它们都是正常的消息。没有任何会议或类似的东西。我什至清理了一些电子邮件。

        Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application();

        NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
        MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

        var mail = myInbox.Items;

        foreach (object items in mail)
        {
            var item = items as MailItem;
            if (item != null)
            {

            if (!senderEmail.Equals(String.Empty) && senderName.Equals(String.Empty) && emailSubject.Equals(String.Empty))
            {
               try
                    {
                        if (((MailItem)item).SenderEmailAddress.ToLower().Contains(senderEmail.ToLower()))
                        {
                            if (count <= 40)
                            {
                                if (((MailItem)item).SenderEmailAddress.Contains(""))
                                {
                                    var senderEmailAddress = ((MailItem)item).SenderEmailAddress.Remove(((MailItem)item).SenderEmailAddress.IndexOf(""), 32);
                                    resultsGrid.Rows.Add(count, ((MailItem)item).Subject, ((MailItem)item).SenderName, senderEmailAddress, ((MailItem)item).CreationTime.ToString());
                                    resultsGrid.AutoResizeColumns();
                                }
                                else if (((MailItem)item).SenderEmailAddress.Contains(""))
                                {
                                    var senderEmailAddress = ((MailItem)item).SenderEmailAddress.Remove(((MailItem)item).SenderEmailAddress.IndexOf(""), 75);
                                    resultsGrid.Rows.Add(count, ((MailItem)item).Subject, ((MailItem)item).SenderName, senderEmailAddress, ((MailItem)item).CreationTime.ToString());
                                    resultsGrid.AutoResizeColumns();
                                }
                                else
                                {
                                    resultsGrid.Rows.Add(count, ((MailItem)item).Subject, ((MailItem)item).SenderName, ((MailItem)item).SenderEmailAddress, ((MailItem)item).CreationTime.ToString());
                                    resultsGrid.AutoResizeColumns();
                                }

                                count++;
                            }
                            else
                            {
                                resultsGrid.Rows.Add(String.Empty, "Total items in      Mailbox: " + myInbox.Items.Count, String.Empty, String.Empty, String.Empty);
                                break;
                            }
                        }
                    }
                    catch (COMException e)
                    {
                        resultsGrid.Rows.Add(e.Message);
                        resultsGrid.AutoResizeColumns();
                        break;
                    }
                    continue;
                }
     }
4

1 回答 1

0

Using GC.Collect(), I was able to force the program to clean up the previous objects/calls to Outlook.

I know it's not the best idea to call GC.Collect(), but I believe it fits the criteria given here: http://blogs.msdn.com/b/ricom/archive/2004/11/29/271829.aspx

The problem, I believe, is that I am generating many requests to open up many different emails and garbage collection usually happens at random it isnt happening fast enough so by the time I reach my limit garbage collection needs to happen. The problem is that it isn't happening before I do so I need to call GC.Collect() every so often to ensure I am resetting those open objects I have.

于 2013-11-06T19:21:22.900 回答