Best way to move a MailItem to another folder.

Add-in Express™ Support Service
That's what is more important than anything else

Best way to move a MailItem to another folder.
Best way to move a MailItem to another folder. 
TheNewCOMAddin Dev




Posts: 26
Joined: 2019-07-11
Hello All,

I am working on a VSTO Addin For Outlook. I have a requirement to move certain items from "Reading Pane" ADX Form (added via ADXFormManager -> ADXFormCollection -> ADXForm) to "Deleted Items" Or "Junk Emails". We (Our Backend Team) have setup MS accounts with notification whenever an item is deleted from inbox folder (MS Graph API: See: https://developer.microsoft.com/en-us/graph/graph-explorer)

Following is the function code snippet I am using. All is well in COM Addin (no error, no visible issues).
However, I am told by our Backend team that the notifications they get for the activity of move gives them incorrect data i.e "parentFolderId" which is actually not present when confirmed by Graph API call the API to list folders https://graph.microsoft.com/v1.0/me/mailfolders.
They report that everything works just fine when we manually move emails from Outlook but not when the COM Addin moves it. Hence wanted to confirm.

1. Is the function below righ way to move a mailItem to another folder ?
2. Any alternative methods to move.

AddinModule.cs :

        public void MoveEmailToFolder(Outlook.MailItem item, String targetFolder = "junk")
        {

            Outlook.NameSpace ns = null;
            Outlook.Explorer currExplorer = null;
            Outlook.MAPIFolder currFolder = null;
            Outlook.Store store = null;
            Outlook.MAPIFolder storeFolder = null;
            Outlook.MAPIFolder tgtFolder = null;
            try
            {
                ns = OutlookApp.Session;
                currExplorer = OutlookApp.ActiveExplorer();
                currFolder = currExplorer.CurrentFolder;
                store = currFolder.Store;

                // If searching Inbox 
                if (targetFolder == "junk")
                {
                    tgtFolder = store.GetDefaultFolder((Outlook.OlDefaultFolders.olFolderJunk));
                    item.Move(tgtFolder);
                }
                else if (targetFolder == "deleted")
                {
                    tgtFolder = store.GetDefaultFolder((Outlook.OlDefaultFolders.olFolderDeletedItems));
                    item.Move(tgtFolder);
                }
            }
            finally
            {
                if (storeFolder != null)
                    Marshal.ReleaseComObject(storeFolder);
                if (store != null)
                    Marshal.ReleaseComObject(store);
                if (currFolder != null)
                    Marshal.ReleaseComObject(currFolder);
                if (currExplorer != null)
                    Marshal.ReleaseComObject(currExplorer);
                if (ns != null)
                    Marshal.ReleaseComObject(ns);
            }
        }
Posted 10 Jan, 2020 07:17:57 Top
Andrei Smolin


Add-in Express team


Posts: 17243
Joined: 2006-05-11
Hello,

TheNewCOMAddin Dev writes:
However, I am told by our Backend team that the notifications they get for the activity of move gives them incorrect data i.e "parentFolderId" which is actually not present when confirmed by Graph API call the API to list folders https://graph.microsoft.com/v1.0/me/mailfolders.


I cannot comment this: I simply know nothing about this.

I see these omissions in your code:

1. tgtFolder should be released.
2. item.Move(tgtFolder) returns a COM object that you should release.
3. I would call item.Delete() instead of moving the item to the Deleted Items folder.
4. I would get the folder using item.Parent; this code doesn't depend on what explorer is active when your method is invoked.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 10 Jan, 2020 08:11:00 Top
TheNewCOMAddin Dev




Posts: 26
Joined: 2019-07-11
Hi Andrei,

Thanks for those comments and corrections! I have now incorporated those.
And, The 'Delete' operation seems to have fixed the notification. We are testing this further.
However, just wondering if there is a direct function to even put items in "Junk Email" without the "MailItem.Move" which seems to do the similar issues with notification as we did with moving items in delete.

Again, thanks for taking the time and replying of this topic.
Posted 13 Jan, 2020 08:45:05 Top
Andrei Smolin


Add-in Express team


Posts: 17243
Joined: 2006-05-11
Hello,

TheNewCOMAddin Dev writes:
However, just wondering if there is a direct function to even put items in "Junk Email" without the "MailItem.Move" which seems to do the similar issues with notification as we did with moving items in delete.


No such method exists in the Outlook object model. Still, you could try to "click" programmatically the built-in ribbon button via {right-click} | Junk | Block Sender. Such a "click" applies to the selected email(s). If required, you can change the selection via Explorer.AddToSelection() and Explorer.ClearSelection(); these methods are available in Outlook 2010 and above.

To "click" that button, you call CommandBars.ExecuteMso("JunkEmailAddToBlockedSendersList"); the string is the IdMso of that button. Obtain a CommandBars object via Explorer.CommandBars. Make sure you release every COM object that you create; Explorer, CommandBars, Selection, every item in the selection are COM objects.

Also, such a "click" may show a dialog message(s). You cannot avoid this. You can close such a dialog using Windows API and/or UI Automation but this is quite a serious task requiring extensive research and testing.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 14 Jan, 2020 02:32:51 Top