在 Exchange Server 上使用联系人

使用 EWS 获取联系人

Aspose.Email 提供了 EWSClient 用于使用 Exchange Web Services 连接 Microsoft Exchange Server 的类。以下代码片段使用 Exchange Web Services 读取所有联系人。以下代码片段展示了如何使用 EWS 获取联系人。

// Create instance of IEWSClient class by giving credentials
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");

// List all the contacts
Contact[] contacts = client.getContacts(client.getMailboxInfo().getContactsUri());
for (Contact contact : contacts) {
    MapiContact mapiContact = Contact.to_MapiContact(contact);
    // Display name and email address
    System.out.println("Name: " + mapiContact.getNameInfo().getDisplayName() + "+ Email Address: " + mapiContact.getElectronicAddresses().getEmail1());
}

使用联系人名称解析联系人

以下代码片段展示了如何使用 EWS 获取联系人

// Create instance of IEWSClient class by giving credentials
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");

// List all the contacts
Contact[] contacts = client.resolveContacts("Changed Name", ExchangeListContactsOptions.FetchPhoto);
for (Contact c : contacts) {
    MapiContact contact = Contact.to_MapiContact(c);
    // Display name and email address
    System.out.println("Name: " + contact.getNameInfo().getDisplayName() + "+ Email Address: " + contact.getElectronicAddresses().getEmail1());
}

确定联系人备注格式

NotesFormat 指定联系人备注文字的格式类型,由 TextFormat 枚举定义。

使用 ID 获取联系人

如以下代码示例所示,可使用联系人的 ID 从服务器检索特定联系人。

Contact fetchedContact = client.getContact(id);

添加联系人

EWSClientcreateContact() 此方法可用于向 Exchange Server 添加联系人信息。 createContact() 方法接受一个 Contact 对象作为输入参数。

向 Exchange Server 添加联系人:

  1. 使用地址和凭据初始化 EWSClient。
  2. 使用所需属性初始化 Contact 对象。
  3. 调用 CreateContact 方法将联系人添加到 Exchange Server。

Aspose.Email 提供了 EWSClient 用于使用 Exchange Web Services 连接 Microsoft Exchange Server 的类。代码片段展示了如何使用 Exchange Web Services 向 Exchange Server 添加联系人。

// Set mailboxURI, Username, password, domain information
String mailboxUri = "https://ex2010/ews/exchange.asmx";
String username = "test.exchange";
String password = "pwd";
String domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);
IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);

// Create New Contact
Contact contact = new Contact();

// Set general info
contact.setGender(Gender.Male);
contact.setDisplayName("Frank Lin");
contact.setCompanyName("ABC Co.");
contact.setJobTitle("Executive Manager");
PhoneNumber tmp0 = new PhoneNumber();
tmp0.setNumber("123456789");
tmp0.setCategory(PhoneNumberCategory.getHome());

// Add Phone numbers
contact.getPhoneNumbers().add(tmp0);
AssociatedPerson tmp1 = new AssociatedPerson();
tmp1.setName("Catherine");
tmp1.setCategory(AssociatedPersonCategory.getSpouse());

// contact's associated persons
contact.getAssociatedPersons().add(tmp1);
AssociatedPerson tmp2 = new AssociatedPerson();
tmp2.setName("Bob");
tmp2.setCategory(AssociatedPersonCategory.getChild());
contact.getAssociatedPersons().add(tmp2);
AssociatedPerson tmp3 = new AssociatedPerson();
tmp3.setName("Merry");
tmp3.setCategory(AssociatedPersonCategory.getSister());
contact.getAssociatedPersons().add(tmp3);
Url tmp4 = new Url();
tmp4.setHref("www.blog.com");
tmp4.setCategory(UrlCategory.getBlog());

// URLs
contact.getUrls().add(tmp4);
Url tmp5 = new Url();
tmp5.setHref("www.homepage.com");
tmp5.setCategory(UrlCategory.getHomePage());
contact.getUrls().add(tmp5);
EmailAddress tmp6 = new EmailAddress();
tmp6.setAddress("Frank.Lin@Abc.com");
tmp6.setDisplayName("Frank Lin");
tmp6.setCategory(EmailAddressCategory.getEmail1());

// Set contact's Email address
contact.getEmailAddresses().add(tmp6);

try {
    client.createContact(contact);
} catch (java.lang.RuntimeException ex) {
    System.out.println(ex.getMessage());
}

更新联系人

联系人信息可以使用 Microsoft Outlook 更新。Aspose.Email 也可以使用 Exchange Web Service (EWS) 更新 Exchange Server 上的联系人信息。 IEWSClient 的 updateContact() 此方法可以更新 Exchange Server 上的联系人信息。

IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);

// List all the contacts and Loop through all contacts
Contact[] contacts = client.getContacts(client.getMailboxInfo().getContactsUri());
Contact contact = contacts[0];
System.out.println("Name: " + contact.getDisplayName());
contact.setDisplayName("David Ch");
client.updateContact(contact);

删除联系人

EWSClient class 提供了 deleteItem 以访问并删除 Exchange Server 联系人文件夹中的联系人。此方法以联系人 ID 作为输入参数。

从 Exchange Server 删除联系人:

  1. 使用地址和凭据初始化 ExchangeWebServiceClient。
  2. 使用其 ID 删除联系人。

以下代码片段展示了如何使用 Exchange Web Service 删除 Exchange 服务器上的联系人。

IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");

String strContactToDelete = "John Teddy";
Contact[] contacts = client.getContacts(client.getMailboxInfo().getContactsUri());
for (Contact contact : contacts) {
    if (contact.getDisplayName().equals(strContactToDelete))
        client.deleteItem(contact.getId().getEWSId(), DeletionOptions.getDeletePermanently());

}
client.dispose();