【问题标题】:Add contact to distribution list programmatically以编程方式将联系人添加到分发列表
【发布时间】:2014-01-10 11:50:40
【问题描述】:

我真的被这个问题困住了,搜索并没有给我带来很多好处。我发现的大多数答案要么让联系人不添加它们,要么使用 LDAP。

我能做的最好的事情是显示将人员添加到分发列表的窗口,但我无法以编程方式完成该部分

这是我最好的尝试:

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
NameSpace oNS = oApp.GetNamespace("MAPI");
//Get Global Address List.
AddressLists oDLs = oNS.AddressLists;
AddressList oGal = oDLs["Global Address List"];
AddressEntries oEntries = oGal.AddressEntries;
AddressEntry oDL = oEntries["MyDistributionList"];

//Get Specific Person
SelectNamesDialog snd = oApp.Session.GetSelectNamesDialog();
snd.NumberOfRecipientSelectors = OlRecipientSelectors.olShowTo;
snd.ToLabel = "D/L";
snd.ShowOnlyInitialAddressList = true;
snd.AllowMultipleSelection = false;
//snd.Display();
AddressEntry addrEntry = oDL;
if (addrEntry.AddressEntryUserType == Microsoft.Office.Interop.Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry)
{
    ExchangeDistributionList exchDL = addrEntry.GetExchangeDistributionList();
    AddressEntries addrEntries = exchDL.GetExchangeDistributionListMembers();

    string name = "John Doe";
    string address = "John.Doe@MyCompany.com";
    exchDL.GetExchangeDistributionListMembers().Add(OlAddressEntryUserType.olExchangeUserAddressEntry.ToString(), name, address);
    exchDL.Update(Missing.Value);
}

使用这个我可以访问分发列表,但我得到“书签无效”异常

exchDL.GetExchangeDistributionListMembers().Add(OlAddressEntryUserType.olExchangeUserAddressEntry.ToString(), name, address);

行。

我可以访问上述列表。

编辑:

【问题讨论】:

  • 您是否尝试将联系人添加到 GAL 分发或联系人文件夹中的 DL?
  • @DmitryStreblechenko 到 GAL

标签: c# com outlook office-interop


【解决方案1】:

问题在于,当您使用 Outlook API 时,您是作为用户使用其功能,而不是作为管理员。
不仅如此,您只能执行可以通过 Outlook UI 执行的操作。

Outlook 不允许您修改通讯组列表,因此您无法使用 Outlook API 进行修改。

有两种可能的方法:

  1. 使用 NetApi 函数 NetGroupAddUserNetLocalGroupAddMembers,具体取决于组是本地组还是全局组。这需要使用 P/Invoke 导入这些函数,并且不适用于通用组。

2。使用 LDAP 查找您需要的组,并将您想要的用户添加到其中。这可以使用 System.DirectoryServices 命名空间来完成,如下所示:

using(DirectoryEntry root = new DirectoryEntry("LDAP://<host>/<DC root DN>"))
using(DirectorySearcher searcher = new DirectorySearcher(root))
{
    searcher.Filter = "(&(objName=MyDistributionList))";
    using(DirectoryEntry group = searcher.findOne())
    {
        searcher.Filter = "(&(objName=MyUserName))";
        using(DirectoryEntry user = searcher.findOne())
        {
             group.Invoke("Add", user.Path);
        }
    }
}

这些只是包装旧的 COM ADSI 接口,这就是我使用 group.Invoke() 的原因。它需要更多练习,但比 NetApi 功能强大得多。

【讨论】:

  • @jimbifd 但我可以使用 Outlook 做到这一点。 :)
  • @AngelicCore 对不起,我想我错过了。您还可以通过 Outlook 管理 GAL,还是只管理您的个人联系人?
  • @jimbifd 我可以访问全球分发列表。除此之外,我只能管理我的个人物品(如预期的那样)。 PS:去编辑Q
  • @AngelicCore 所以我认为我的回答仍然相关:)
  • @jimbifd 但是怎么做呢? LDAP 很有用,但现在我无权访问它。这就是为什么我尝试使用另一种方法 - 互操作来做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多