在项目过程中,遇到需要操作outlook信息的相关技术,遍寻,找到一些有用的。
C#操作outlook需要用到Microsoft.Office.Interop.Outlook.dll,这个文件可在微软官网寻找office PIA安装获得。
通过Alias获取用户名:
private string GetUserName(string alias)
{
Application outLookApp = new Application();
Recipient rcp = outLookApp.Session.CreateRecipient(alias);
rcp.Resolve();
return rcp.Name;
}
通过用户名获取相关信息:
Application outLookApp = new Application();
NameSpace ns = outLookApp.GetNamespace("MAPI");
AddressLists aLs = ns.AddressLists;
AddressList aL = ns.GetGlobalAddressList();
AddressEntries aEs = aL.AddressEntries;
for (int i = 1; i < aEs.Count; i++)
{
AddressEntry aE = aEs[i]; //可直接通过:aEs["UserName"]获取用户信息,但此信息不全,可用下面exchangeuser获取详细信息。
if (aE != null)
{
。。。
}
}
//获取当前用户信息
AddressEntry currentUser = aEs.Session.CurrentUser.AddressEntry;
if (currentUser.Type == "EX")
{
ExchangeUser exchUser = currentUser.GetExchangeUser();
if (exchUser != null)
{
AddressEntries addrEntries = exchUser.GetMemberOfList();//获取用户群组
if (addrEntries != null)
{
foreach (AddressEntry addrEntry in addrEntries)
{
Console.WriteLine(addrEntry.Name);
}
}
Console.WriteLine(exchUser.Alias);//获取用户alias,相关详细信息均可通过exchUser获得
}
}
更多详细信息可参考: