none
items_ItemAdd event runs multiple times when used on shared mailbox - Outlook C# RRS feed

  • Question

  • Hi all

    I have created Outlook add-in where I am checking for every new email coming to shared mailbox.

    The add in is used by multiple user on shared mailbox. The add-in will check whether email is a reply or not; if it is then it will move the email to folder X. But the issue is, there are two copies of each email. I have a feeling that since it is used by multiple users, user A sync with the server first, receives new email and email is moved to folder X. Now user B will sync with server, receives the same email, add-in will do the same thing since all users work with cached mode ON.

    Here is my code, any help will be appreciated.

    Thank you.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {

                Outlook.NameSpace ns = Globals.ThisAddIn.Application.Session;
                Outlook.MAPIFolder inbox = ns.
                GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

                outlookNameSpace = this.Application.GetNamespace("MAPI");
                string recipientName = "abc@abcd.com";
                Outlook.Recipient recip = outlookNameSpace.CreateRecipient(recipientName);
                recip.Resolve();
                if (recip.Resolved)
                {
                    inbox = outlookNameSpace.GetSharedDefaultFolder(recip, Outlook.OlDefaultFolders.olFolderInbox);
                    items = inbox.Items;
                    items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
                }

            }

            void items_ItemAdd(object Item)
            {
                if (Item != null)
                {
                    Outlook.MailItem mail = Item as Outlook.MailItem;
                    if (mail.MessageClass == "IPM.Note")
                    {
                        try
                        {
                            Outlook.Application app = Globals.ThisAddIn.Application;
                            if (Item is Outlook.MailItem)
                            {
                                if (mailItem.ConversationIndex.Length >= 64)
                                {
                                    Outlook.Folder nonInbox = folder.Parent as Outlook.Folder;
                                    string FolderName = "Reply";

                                    Outlook.MAPIFolder DestFolder = null;
                                    try
                                    {
                                        DestFolder = nonInbox.Folders[FolderName];
                                    }
                                    catch
                                    {
                                        
                                    }
                                    try
                                    {
                                        mailItem.Move(DestFolder);
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message);
                                    }
                                }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }

    Wednesday, November 13, 2019 10:00 AM