在控制台应用程序中使用C#将成员添加到Outlook GAL通讯组列表
我正在尝试编写一个C#控制台应用程序,它可以通过编程方式更新全局地址列表(GAL)中的Outlook通讯组列表(DL)。我有权更新此DL。我可以使用Outlook在PC上以交互方式执行此操作,也可以使用在控制台应用程序中使用C#将成员添加到Outlook GAL通讯组列表,c#,visual-studio,outlook,gal,distribution-list,C#,Visual Studio,Outlook,Gal,Distribution List,我正在尝试编写一个C#控制台应用程序,它可以通过编程方式更新全局地址列表(GAL)中的Outlook通讯组列表(DL)。我有权更新此DL。我可以使用Outlook在PC上以交互方式执行此操作,也可以使用Win32::NetAdmin::GroupAddUsers在Perl代码中执行此操作 添加对COM库“Microsoft Outlook 14.0对象库”的引用后,然后通过以下方式访问: using Outlook = Microsoft.Office.Interop.Outlook; 我可以
Win32::NetAdmin::GroupAddUsers
在Perl代码中执行此操作
添加对COM库“Microsoft Outlook 14.0对象库”的引用后,然后通过以下方式访问:
using Outlook = Microsoft.Office.Interop.Outlook;
我可以成功地读取DL,甚至可以在正在搜索的“主”DL中递归DL。以下是工作代码(本文不需要评论):
定义了Members.Add()
的参数,代码中显示的值完全来自于从另一个DL检查自己的成员对象
显示的异常只是“书签无效”。之前曾询问过一个问题,但解决方案是使用p/Invoke或LDAP。我真的不知道如何使用P/Invoke(严格地说是C#和Perl程序员,而不是Windows/C/C++程序员),而且我没有访问LDAP服务器的权限,所以我真的想通过Microsoft.Office.Interop.Outlook
对象来实现这一点
非常感谢您的帮助 在尝试了几个不同的.NET对象之后,使用中发布的
System.DirectorServices.AccountManagement
最终为我编写了这个工作。结束我自己的问题
private static List<Outlook.AddressEntry> GetMembers(string dl, bool recursive)
{
try
{
List<Outlook.AddressEntry> memberList = new List<Outlook.AddressEntry>();
Outlook.Application oApp = new Outlook.Application();
Outlook.AddressEntry dlEntry = oApp.GetNamespace("MAPI").AddressLists["Global Address List"].AddressEntries[dl];
if (dlEntry.Name == dl)
{
Outlook.AddressEntries members = dlEntry.Members;
foreach (Outlook.AddressEntry member in members)
{
if (recursive && (member.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry))
{
List<Outlook.AddressEntry> sublist = GetMembers(member.Name, true);
foreach (Outlook.AddressEntry submember in sublist)
{
memberList.Add(submember);
}
}
else {
memberList.Add(member);
}
}
}
else
{
Console.WriteLine("Could not find an exact match for '" + dl + "'.");
Console.WriteLine("Closest match was '" + dlEntry.Name +"'.");
}
return memberList;
}
catch
{
// This mostly fails if running on a PC without Outlook.
// Return a null, and require the calling code to handle it properl
// (or that code will get a null-reference excception).
return null;
}
}
private static void AddMembers(string dl)
{
Outlook.Application oApp = new Outlook.Application();
Outlook.AddressEntry ae = oApp.GetNamespace("MAPI").AddressLists["Global Address List"].AddressEntries[dl];
try {
ae.Members.Add("EX", "Tuttle, James", "/o=EMC/ou=North America/cn=Recipients/cn=tuttlj");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
ae.Update();
}