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

问题描述:

我有我的Outlook 2010,外接(C#),许多文件夹。他们在我的私人邮箱或我的共享邮箱中。如何获取文件夹项目关联的发件人电子邮件地址? (VSTO /展望2010/MAPI)

现在我正在寻找一个解决方案找出来,如何获得一个专用文件夹相关联的权限的电子邮件地址(发件人/接受者)。它可以是我的私人或任何我的共享邮箱中的任何文件夹。

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

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

+0

你的意思是你绑找出父Exchange邮箱的主人吗? –

+0

是的,我的意思是。 – creg

我喜欢回答我自己的问题:我想我已经找到一个合理的解决方案。

我不把里面的函数任何异常,我这样做,从外面。

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

PR_MAILBOX_OWNER_ENTRYID只在在线模式。缓存模式不公开它。 –

+0

感谢您的注意,我们不使用缓存模式。 – creg

你可以尝试

  1. 检索使用Store.PropertyAccessor.GetProperty店里PR_MAILBOX_OWNER_ENTRYID属性,但它仅通过在线商店暴露,缓存店不公开它。

  2. 解析Exchange存储条目标识以提取商店所有者的DN,然后创建GAL用户条目标识。

  3. 你可以看一下配置文件数据,找到店老板条目ID。

如果使用Redemption是一个选项,它暴露了RDOExchangeMailboxStore .Owner属性:

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