简体   繁体   中英

Error trying to pull sender email address from microsoft outlook 2010 and comparing it to a string

The below code works 9 out of ten times, but in some cases I get the error of: Your server administrator has limited the number of items you can open simultaneously. Try closing messages you have opened or removing attachments and images from unsent messages you are composing.

I checked the email it's trying to pull and they are all just normal messages. There isn't any meetings or anything like that. I even cleaned some of the emails out of that sort.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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