添加联系人到通讯组列表编程(Add contact to distribution list pro

2019-10-19 03:55发布

我真的卡在这个问题上和搜索没有取得我很多。 我发现无论是最得到的答案并不联系人添加它们或使用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);

线。

我说,名单上的访问。

编辑:

Answer 1:

问题是,当你使用Outlook API,你使用它的功能作为一个用户,而不是作为管理员。
更重要的是, 你只能做的事情,你可以通过Outlook用户界面做的。

Outlook不允许您修改通讯组列表,所以你将无法使用Outlook API来做到这一点。

有2点可能的方式来做到这一点:

  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的功能更加强大。



文章来源: Add contact to distribution list programmatically