ashwani gupta
ashwani gupta

Reputation: 150

Can i run outlook add-on method in background process when load function call?

This is my code there is two method one is notify mail,it call when new mail comes in outlook. And second method is mailProcessTrigger it perform some change in mail. i want to run mailProcessTrigger() method another thread and it call every 5 minutes. I am unable to make that method to multi-thread.

namespace PhishCOP
    {
        public partial class Phishing
        {
            Outlook.Application outlookApp;
            Outlook.MAPIFolder deleterFolder = null;
            Outlook.Items mailItems = null;
            Outlook.MAPIFolder inboxFolder = null;
            Outlook.Items items;
            string SchemaTransportHeader = @"http://schemas.microsoft.com/mapi/proptag/0x007D001E";
            const string PR_ATTACH_DATA_BIN = "http://schemas.microsoft.com/mapi/proptag/0x37010102";

    private void Phishing_Load(object sender, RibbonUIEventArgs e)
        {
            outlookApp = new Outlook.Application();
            inboxFolder = outlookApp.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            deleterFolder = outlookApp.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk);
            mailItems = inboxFolder.Items;//.Restrict("[Unread]=true");
            mailItems.Sort("[ReceivedTime]", true);
            AddACategory();
            items = inboxFolder.Items;

            items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(CallFunction);

           }


        private void CallFunction(object data)
        {
           //move mail to another folder
        }

        private void mailProcessTrigger()
        {
        //mail process
        }
   }
}

Upvotes: 0

Views: 301

Answers (1)

Eric Legault
Eric Legault

Reputation: 5834

Items in the Outlook Object Model cannot be used in background threads and will lead to instability in your add-in and could cause it and Outlook to crash. You can only use Extended MAPI with C++ or Redemption to run multi-threaded Outlook solutions.

Upvotes: 2

Related Questions