Outlook C# move an email into a mailbox associated with a MS365 Group

Viewed 160

I am writing a C# VSTO addin to provide some email filing helper functions. I am stuck as to how can programmatically move an email I have received into the conversations mailbox associated with an MS365 Group. The group itself is created as part of a sharepoint modern team site. I can manually drag emails over to the group mailbox, but I cannot find a way to obtain the folder object in C#.

Microsoft® Outlook® for Microsoft 365 MSO (Version 2111 Build 16.0.14701.20254) 64-bit.

1 Answers

You can use the Move method of Outlook items.


private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.NewMail += new Microsoft.Office.Interop.Outlook.
        ApplicationEvents_11_NewMailEventHandler
        (ThisAddIn_NewMail);

}

private void ThisAddIn_NewMail()
{
    Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)this.Application.
        ActiveExplorer().Session.GetDefaultFolder
        (Outlook.OlDefaultFolders.olFolderInbox);
    Outlook.Items items = (Outlook.Items)inBox.Items;
    Outlook.MailItem moveMail = null;
    items.Restrict("[UnRead] = true");
    Outlook.MAPIFolder destFolder = inBox.Folders["Test"];
    foreach (object eMail in items)
    {
        try
        {
            moveMail = eMail as Outlook.MailItem;
            if (moveMail != null)
            {
                string titleSubject = (string)moveMail.Subject;
                if (titleSubject.IndexOf("Test") > 0)
                {
                    moveMail.Move(destFolder);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

I suppose the destination folder is listed in Outlook in an additional store. So, you can use the Stores property of the Namespace class to find the target folder.

Related