Exchange サーバー上の連絡先の操作
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);
連絡先の追加
この EWSClient クラス createContact() メソッドは、Contact 情報を Exchange Server に追加するために使用できます。 createContact() メソッドは 連絡先 オブジェクトを入力パラメータとして。
Exchange Server に連絡先を追加するには:
- アドレスと資格情報で EWSClient を初期化します。
- 必要なプロパティで Contact オブジェクトを初期化します。
- 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 から連絡先を削除するには:
- アドレスと資格情報で ExchangeWebServiceClient を初期化します。
- 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();