以编程方式将联系人添加到通讯组列表 [英] Add contact to distribution list programmatically

查看:605
本文介绍了以编程方式将联系人添加到通讯组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的被困在这个问题,搜索没有产生我很多。大多数回答我发现要么得到联系人不添加他们或使用LDAP。



我能做的最好的是显示窗口,你添加人到通讯组列表




这是我最好的尝试:

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
NameSpace oNS = oApp.GetNamespace(MAPI);
//获取全局地址列表。
AddressLists oDLs = oNS.AddressLists;
AddressList oGal = oDLs [全局地址列表];
AddressEntries oEntries = oGal.AddressEntries;
AddressEntry oDL = oEntries [MyDistributionList];

//获取特定人员
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); 

行。





编辑:

解决方案

事情是,当您使用Outlook API时,您将其功能用作用户,而不是管理员。 您不能透过Outlook UI执行某些操作。修改分发列表,因此您将无法使用outlook API执行此操作。



有2种可能的方法:


  1. 使用NetApi函数 NetGroupAddUser NetLocalGroupAddMembers ,具体取决于组是本地组还是全局组。这需要使用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);
}
}
}

ADSI接口,这就是为什么我使用group.Invoke()。它需要一些更多的实践,但是比NetApi功能更强大。


I am really stuck in this issue and searching didn't yield me a lot. Most answers I found either get Contacts not add them or use LDAP.

The best I've been able to do is display the window where you add people to the distribution list but I am not able to do that part programmatically

Here is the my best attempt:

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

Using this i can access the Distribution List but I get "The bookmark is not valid" exception on the

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

line.

I have access on said list.

EDIT:

解决方案

The thing is that when you use the Outlook API, you use its functionality as a user, not as an admin.
More than that, you can only do things that you can do through Outlook UI.

Outlook doesn't allow you to modify distribution lists, so you won't be able to do it using the outlook API.

There are 2 possible ways to do it:

  1. Use the NetApi functions NetGroupAddUser or NetLocalGroupAddMembers, depending on whether the group is a local or global group. This will require importing those functions with P/Invoke and won't work on universal groups.

2. Use LDAP to find the group you need, and add the users you want to it. This can be done using the System.DirectoryServices namespace like this:

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

These just wrap the old COM ADSI interfaces, that's why I use group.Invoke(). It takes a bit more practice, but is much more powerful than the NetApi functions.

这篇关于以编程方式将联系人添加到通讯组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆