Search code examples
c#outlookinteropoffice-interop

Find email-enabled public folders exclusively using "Microsoft.Office.Interop.Outlook"


Is there a way to exclusively identify email-enabled public folders using Microsoft.Office.Interop.Outlook? EWS or PowerShell should not be used. The program should run on the workstation and utilize the installed Outlook.

Once I've found an email-enabled public folder, I should list the emails contained within it.

So far, I'm encountering difficulties. For instance, I have the following method:

// Recursive method for listing the email addresses of email-enabled public folders
static void ListPublicFolders(Outlook.Folder? folder, string indent)
{
    if (folder != null)
    {
        foreach (object obj in folder.Folders)
        {
            if (obj is Outlook.Folder)
            {
                Outlook.Folder? subFolder = obj as Outlook.Folder;

                if (subFolder != null && subFolder.DefaultItemType == Outlook.OlItemType.olMailItem)
                {
                    Outlook.MAPIFolder? parentFolder = subFolder.Parent as Outlook.MAPIFolder;
                    string              parentName   = parentFolder != null ? parentFolder.Name : "Parent folder not found";

                    Console.WriteLine($"{indent}- {subFolder.Name}: {parentName}");

                    if (parentFolder != null)
                    {
                        Marshal.ReleaseComObject(parentFolder);
                    }
                }

                ListPublicFolders(subFolder, indent + "  ");

                if (subFolder != null)
                {
                    Marshal.ReleaseComObject(subFolder);
                }
            }
        }
    }
}

The query

if (subFolder != null && subFolder.DefaultItemType == Outlook.OlItemType.olMailItem)

fails because subFolder.DefaultItemType returns the value Outlook.OlItemType.olPostItem, even though the public folder was set up as an email-enabled folder in Exchange.

Specifically, this is in Microsoft 365. In the Exchange admin center, when creating the folder, I explicitly checked the box for "Email-enabled." This action resulted in two additional options: "Delegation" and "Email properties." In "Email properties," I can specify an alias and a display name. By default, both fields are set to "Orders." Now, I expect the public folder to be email-enabled, with the email address orders@domain.tld.

I don't understand why Outlook is treating the folder incorrectly (I can only create posts and not send emails).

Perhaps someone can help me figure this out.

Thank you and best regards,

René

I want to determine the email-enabled public folders exclusively using "Microsoft.Office.Interop.Outlook".


Solution

  • Public Folders in Exchange that are mail-enabled are a 2-part object. One part is the folder in the hierarchy and the other part is an Active Directory object. This is true even in Office365.

    When a public folder is mail-enabled, exchange creates a new AD object and applies a default email address to it. After the AD object is created, exchange pulls the object's "objectGuid" (that's the ldap property name) and writes that onto the hierarchy folder.

    This creates a loose link between the hierarchy folder and the AD object.

    This value is written to the mapi property ID: PR_PF_PROXY [0x671D0102]. This is a binary value that will have the 16 bytes of the guid.

    Using the OOM (Outlook object model), you can get to mapi properties like this:

               PropertyAccessor propertyAccessor = theFolder.PropertyAccessor;
                string PR_PF_PROXY = "http://schemas.microsoft.com/mapi/proptag/0x0671d0102";
                guidBytes byte[] = propertyAccessor.GetProperty(PR_ENTRYID);
    

    if guidBytes is non-null/non-empty/non-zero, then the folder might be mail-enabled.

    I say might be because of the loosely coupled nature here. It is possible and common for the guid stored on the hierarchy folder to not match any AD object. In such a case, the folder looks mail-enabled, but there's no way to get to the AD object. It's also possible for mail to still deliver to the folder even when this link is broken because Exchange also creates a link on the AD object that points at the public folder, but it's a different value all together. So, even though the guid might not match to an AD object, the AD object might properly point to the hierarchy folder.

    As such, it is not only difficult to determine accurately if a hierarchy folder is really mail-enabled but cannot really be done completely by using only Outlook/mapi.

    For more info about this headache, i suggest this article: https://www.priasoft.com/the-secret-challenges-of-mail-enabled-public-folders-in-microsoft-exchange/