0
votes

I have in my Outlook 2010-Add-In (c#) many folders. They are in my private post box or in one of my shared post boxes.

Now I am looking for a solution to find out, how to get the right email address (sender / recipient) associated with a dedicated folder. It could be any folder from my private or anyone of my shared post boxes.

I think, maybe I could use the EntryId / StoreId from the folder item to identify the corresponding email address.

I know already, that I could get the email address from any mail item but I'm not looking for this solution.

2
Do you mean you are tying to find out the owner of the parent Exchange mailbox? - Dmitry Streblechenko
Yes, I exactly mean that. - creg

2 Answers

0
votes

I like to answer my own questions: I think that I've found a plausible solution.

I do not treat any exceptions inside the function, I do that from outside.

private string GetSMTPAddressByFolderItem(Outlook.MAPIFolder mapiFolder)
    {

        string PR_MAILBOX_OWNER_ENTRYID = @"http://schemas.microsoft.com/mapi/proptag/0x661B0102";
        string PR_SMTP_ADDRESS = @"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";

        Outlook.Store store = null;
        Outlook.NameSpace ns = null;
        Outlook.AddressEntry sender = null;
        Outlook._ExchangeUser exchUser = null;

        try
        {
            if (mapiFolder == null)
            {
                return null;
            }

            // Get the parent store.
            store = mapiFolder.Store;

            string storeOwnerEntryId = store.PropertyAccessor.BinaryToString(store.PropertyAccessor.GetProperty(PR_MAILBOX_OWNER_ENTRYID)) as string;
            ns = Application.GetNamespace(Constants.OL_NAMESPACE); // i.e. "MAPI"

            sender = ns.GetAddressEntryFromID(storeOwnerEntryId);

            if (sender != null)
            {
                if (sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry ||
                    sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
                {
                    exchUser = sender.GetExchangeUser();

                    if (exchUser != null)
                    {
                        return exchUser.PrimarySmtpAddress;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS) as string;
                }
            }

            return null;
        }
        finally
        {
            if (ns != null)
            {
                Marshal.ReleaseComObject(ns);
                ns = null;
            }
            if (store != null)
            {
                Marshal.ReleaseComObject(store);
                store = null;
            }
            if (sender != null)
            {
                Marshal.ReleaseComObject(sender);
                sender = null;
            }
            if (exchUser != null)
            {
                Marshal.ReleaseComObject(exchUser);
                exchUser = null;
            }
        }
    }
0
votes

You can try to

  1. retrieve the PR_MAILBOX_OWNER_ENTRYID property from the store using Store.PropertyAccessor.GetProperty, but it is only exposed by the online stores, cached stores do not expose it.

  2. Parse the Exchange store entry id to extract the DN of the store owner and then create a GAL user entry id.

  3. You can look at the profile data to find the store owner entry id.

If using Redemption is an option, it exposes the RDOExchangeMailboxStore.Owner property:

  skPrimaryExchangeMailbox = 3
  skDelegateExchangeMailbox = 4
  set Session = CreateObject("Redemption.RDOSession")
  Session.MAPIOBJECT = Application.Session.MAPIOBJECT
  for each Store in Session.Stores
    If (Store.StoreKind = skPrimaryExchangeMailbox) or (Store.StoreKind = skDelegateExchangeMailbox) Then
      MsgBox "Store " & Store.Name & " is owned by " &  Store.Owner.Name
    End If
  next