Exchange Web服务创建联系人并添加到全局地址列表

时间:2015-06-16 07:50:02

标签: c# exchangewebservices

我正在尝试使用EWS将外部联系人添加到全局地址列表中。到目前为止,我没有这样做。事件新添加的联系人在用户主要联系人列表中不可见。这里有什么我想念的吗?我试过两个版本。一个使用EWS,如下:

var service = InitConnection(exchangeUrl, exchangeUsername);
        if (service == null)
        {
            return "Failed to connect to exchange server";
        }

        try
        {
            var data = (Hashtable)contactKeyValues;
            if (data == null && data.Count <= 0)
            {
                return "The data can't be extracted. Please check your dictionary parameter of properties.";
            }

            var localExchangeContactObj = ValidateAndBuildContact(data);

            var remoteExchangeContactObj = new Contact(service);

            remoteExchangeContactObj.GivenName = localExchangeContactObj.FirstName;
            remoteExchangeContactObj.Surname = localExchangeContactObj.LastName;
            remoteExchangeContactObj.Initials = localExchangeContactObj.Initials;
            remoteExchangeContactObj.FileAsMapping = FileAsMapping.GivenNameSpaceSurname;

            remoteExchangeContactObj.PhoneNumbers[PhoneNumberKey.BusinessPhone] = localExchangeContactObj.PhoneBusiness;
            remoteExchangeContactObj.PhoneNumbers[PhoneNumberKey.HomePhone] = localExchangeContactObj.PhoneHome;

            remoteExchangeContactObj.EmailAddresses[EmailAddressKey.EmailAddress1] = localExchangeContactObj.Email;

            var address = new PhysicalAddressEntry();
            address.Street = localExchangeContactObj.Address;
            address.City = localExchangeContactObj.City;
            address.State = localExchangeContactObj.State;
            address.PostalCode = localExchangeContactObj.Zip;
            address.CountryOrRegion = localExchangeContactObj.Country;

            remoteExchangeContactObj.PhysicalAddresses[PhysicalAddressKey.Business] = address;
            remoteExchangeContactObj.Body = new MessageBody(BodyType.HTML, localExchangeContactObj.Notes);
            remoteExchangeContactObj.Save(WellKnownFolderName.Contacts);
            return remoteExchangeContactObj.Id.UniqueId.ToString();


        }
        catch (Exception exception)
        {
            return string.Format("Something went wrong while creating contact in exchnage. Here is the details {0}", exception.ToString());
        }

另一种方法是通过LDAP,在这种情况下,它会在当前OU中创建联系人,但在搜索时不会显示。代码如下:

try
        {
            DirectoryEntry de;
            if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
            {
                de = new DirectoryEntry(ldapPath);
            }
            else
            {
                de = new DirectoryEntry(ldapPath, username, password);
            }
            var newContact = de.Children.Add("CN=" + name, "contact");
            newContact.Properties["mail"].Value = email;

            de.CommitChanges();

        }
        catch (COMException comEx)
        {
            var eventLog = new EventLog { Source = "Exchange Integration Create Contact" };
            eventLog.WriteEntry(string.Format("Exception while creating contact {0} ", comEx), EventLogEntryType.Error);

            return comEx.ErrorCode.ToString();
        }
        catch (Exception exception)
        {
            var eventLog = new EventLog { Source = "Exchange Integration Create Contact" };
            eventLog.WriteEntry(string.Format("Exception while creating contact {0} ", exception), EventLogEntryType.Error);

            return exception.ToString();
        }

        return "Contact created";

1 个答案:

答案 0 :(得分:2)

您无法使用EWS在GAL中创建联系人,EWS是一个邮箱访问API,因此您可以在邮箱的联系人文件夹中创建联系人文件夹,这是您的代码尝试执行的操作。但是,这些不会出现在全局地址列表中。创建将出现在GAL中的联系人的唯一受支持方式是使用Exchange命令行管理程序New-MailConact https://technet.microsoft.com/en-us/library/bb124519(v=exchg.150).aspx cmdlet,并在托管代码中使用远程Powershell https://msdn.microsoft.com/en-us/library/office/ff326159(v=exchg.150).aspx。这将创建一个启用邮件的AD联系人。 (您的LDAP代码正在创建一个联系人,但如果您真的想要沿着您需要设置更多属性的路线(如TargetAddress等)查看现有的已启用Mail的联系人,则不会启用邮件。