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

Exchange Web Service Create a contact and add to global address list

我正在尝试使用 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";

您不能使用 EWS 在 GAL 中创建联系人,EWS 是一个邮箱访问 API 因此您可以在邮箱的联系人文件夹中创建联系人,这正是您的代码正在尝试执行的操作。但是,这些不会出现在全局地址列表中。创建将出现在 GAL 中的联系人的唯一受支持方法是使用 Exchange Management Shell New-MailConact https://technet.microsoft.com/en-us/library/bb124519(v=exchg.150).aspx cmdlet and use Remote Powershell in your Managed code with https://msdn.microsoft.com/en-us/library/office/ff326159(v=exchg.150).aspx。这将创建启用邮件的 AD 联系人。 (您的 LDAP 代码正在创建一个联系人,但如果您真的想沿着这条路线走下去,它不会启用邮件,您需要设置更多属性,例如 TargetAddress 等,查看现有的启用邮件的联系人)