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


    }
}

  

 

转载于:https://www.cnblogs.com/suntime/p/10608929.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值