C# 使用Exchange WebService读取联系人
首先下载Microsoft.Exchange.WebServices.dll,如果安装有Exchange服务器,服务器上应该也是有的
Microsoft Exchange Web Services (EWS Managed API)
using Microsoft.Exchange.WebServices.Data; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace BaseClass.Common { public class ExchangeAPI { private static ExchangeService service; /// <summary> /// 初始化服务 /// </summary> /// <returns></returns> private static ExchangeService InitService() { if(service == null) { service = new ExchangeService(Microsoft.Exchange.WebServices.Data.ExchangeVersion.Exchange2007_SP1); service.Credentials = new WebCredentials("用户名", "密码", "域名"); service.AutodiscoverUrl("邮箱地址"); } return service; } /// <summary> /// 获取组下的人 /// </summary> /// <param name="groupAddress"></param> /// <returns></returns> public static DataTable GetContacts(string groupAddress) { DataTable table = new DataTable(); table.Columns.Add("Id", typeof(string)); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Address", typeof(string)); table.Columns.Add("EmailType", typeof(string)); ExchangeService service = InitService(); ExpandGroupResults groupresult = service.ExpandGroup(groupAddress); foreach (EmailAddress email in groupresult.Members) { switch (email.MailboxType) { case MailboxType.Contact: case MailboxType.Mailbox: { DataRow row = table.NewRow(); table.Rows.Add(row); row["Id"] = email.Id; row["Name"] = email.Name; row["Address"] = email.Address; row["EmailType"] = "Contact"; } break; } } return table; } /// <summary> /// 获取组下的分组 /// </summary> /// <param name="groupAddress"></param> /// <returns></returns> public static DataTable GetGroups(string groupAddress) { DataTable table = new DataTable(); table.Columns.Add("Id", typeof(string)); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Address", typeof(string)); table.Columns.Add("EmailType", typeof(string)); ExchangeService service = InitService(); ExpandGroupResults groupresult = service.ExpandGroup(groupAddress); foreach (EmailAddress email in groupresult.Members) { switch (email.MailboxType) { case MailboxType.ContactGroup: case MailboxType.GroupMailbox: case MailboxType.PublicFolder: case MailboxType.PublicGroup: { DataRow row = table.NewRow(); table.Rows.Add(row); row["Id"] = email.Id; row["Name"] = email.Name; row["Address"] = email.Address; row["EmailType"] = "Group"; } break; } } return table; } /// <summary> /// 搜索联系人 /// </summary> /// <param name="name"></param> /// <returns></returns> public static DataTable ResolveName(string name) { DataTable table = new DataTable(); table.Columns.Add("Id", typeof(string)); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Address", typeof(string)); table.Columns.Add("EmailType", typeof(string)); ExchangeService service = InitService(); NameResolutionCollection nameResolutions = service.ResolveName(name, ResolveNameSearchLocation.DirectoryThenContacts, true); foreach (NameResolution resolution in nameResolutions) { switch (resolution.Mailbox.MailboxType) { case MailboxType.ContactGroup: case MailboxType.GroupMailbox: case MailboxType.PublicFolder: case MailboxType.PublicGroup: { DataRow row = table.NewRow(); table.Rows.Add(row); row["Id"] = resolution.Mailbox.Id; row["Name"] = resolution.Mailbox.Name; row["Address"] = resolution.Mailbox.Address; row["EmailType"] = "Group"; } break; case MailboxType.Contact: case MailboxType.Mailbox: { DataRow row = table.NewRow(); table.Rows.Add(row); row["Id"] = resolution.Mailbox.Id; row["Name"] = resolution.Mailbox.Name; row["Address"] = resolution.Mailbox.Address; row["EmailType"] = "Contact"; } break; } } return table; } } }