如何获取文件夹项目关联的发件人电子邮件地址?(VSTO / Outlook 2010 / MAPI)

本文关键字:电子邮件地址 VSTO MAPI Outlook 2010 获取 何获取 文件夹 项目 关联 | 更新日期: 2023-09-27 18:34:28

我的Outlook 2010-Add-In (c#(中有许多文件夹。它们在我的私人邮箱或我的共享邮箱之一中。

现在我正在寻找一种解决方案来找出如何获得与专用文件夹关联的正确电子邮件地址(发件人/收件人(。它可以是我的私人文件夹或我共享邮箱中的任何人。

我想,也许我可以使用文件夹项目中的EntryId/StoreId来识别相应的电子邮件地址。

我已经知道,我可以从任何邮件中获取电子邮件地址,但我不是在寻找这个解决方案。

如何获取文件夹项目关联的发件人电子邮件地址?(VSTO / Outlook 2010 / MAPI)

我喜欢回答我自己的问题:我认为我找到了一个合理的解决方案。

我不处理函数内部的任何异常,我从外部处理

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;
            }
        }
    }