Bekerja dengan Kontak di Server Exchange

Mendapatkan Kontak dengan EWS

Aspose.Email menyediakan EWSClient kelas untuk terhubung ke Microsoft Exchange Server menggunakan Exchange Web Services. Potongan kode berikut menggunakan Exchange Web Services untuk membaca semua kontak. Potongan kode berikut menunjukkan cara mendapatkan Kontak dengan 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());
}

Menyelesaikan Kontak menggunakan Nama Kontak

Potongan kode berikut menunjukkan cara menggunakan get contacts dengan 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());
}

Menentukan Format Catatan Kontak

NotesFormat menentukan tipe format teks catatan kontak yang didefinisikan oleh enumerator TextFormat.

Ambil Kontak menggunakan Id

Kontak tertentu dapat diambil dari server menggunakan ID kontaknya seperti yang ditunjukkan dalam contoh kode berikut.

Contact fetchedContact = client.getContact(id);

Menambahkan Kontak

The EWSClient kelas createContact() metode dapat digunakan untuk menambahkan informasi Kontak ke Server Exchange. createContact() metode mengambil sebuah Kontak objek sebagai parameter input.

Untuk menambahkan kontak ke Server Exchange:

  1. Inisialisasi EWSClient dengan alamat dan kredensial.
  2. Inisialisasi objek Contact dengan properti yang diinginkan.
  3. Panggil metode CreateContact untuk menambahkan kontak ke Server Exchange.

Aspose.Email menyediakan EWSClient kelas untuk terhubung ke Microsoft Exchange Server menggunakan Exchange Web Services. Potongan kode menunjukkan cara menggunakan Exchange Web Services untuk menambahkan kontak ke Server Exchange.

// 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());
}

Memperbarui Kontak

Informasi kontak dapat diperbarui menggunakan Microsoft Outlook. Aspose.Email juga dapat memperbarui informasi kontak di Server Exchange menggunakan Exchange Web Service (EWS). IEWSClient’s updateContact() metode dapat memperbarui informasi kontak di Server Exchange.

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

Menghapus Kontak

The EWSClient class menyediakan deleteItem untuk mengakses dan menghapus kontak dari folder kontak Server Exchange. Metode ini mengambil ID kontak sebagai parameter input.

Untuk menghapus kontak dari Server Exchange:

  1. Inisialisasi ExchangeWebServiceClient dengan alamat dan kredensial.
  2. Hapus kontak menggunakan ID-nya.

Potongan kode berikut menunjukkan cara menghapus kontak dari server Exchange menggunakan layanan web 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();