ทำงานกับรายชื่อบน Exchange Server
ดึงรายชื่อด้วย EWS
Aspose.Email ให้ EWSClient คลาสเพื่อเชื่อมต่อกับ Microsoft Exchange Server โดยใช้ Exchange Web Services โค้ดส่วนต่อไปนี้ใช้ Exchange Web Services เพื่ออ่านรายชื่อทั้งหมด โค้ดส่วนต่อไปนี้จะแสดงวิธีดึง Contacts ด้วย 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());
}
ค้นหารายชื่อโดยใช้ชื่อติดต่อ
โค้ดส่วนต่อไปนี้จะแสดงวิธีใช้ get contacts กับ 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
รายชื่อเฉพาะสามารถดึงจากเซิร์ฟเวอร์โดยใช้ contact id ของมันตามตัวอย่างโค้ดต่อไปนี้.
Contact fetchedContact = client.getContact(id);
เพิ่มรายชื่อ
นี้ EWSClient คลาส createContact() เมธอดนี้สามารถใช้เพื่อเพิ่มข้อมูลรายชื่อไปยัง Exchange Server. ส่วน createContact() เมธอดรับ รายชื่อ ออบเจกต์เป็นพารามิเตอร์อินพุต.
เพื่อเพิ่มรายชื่อไปยัง Exchange Server:
- เริ่มต้น EWSClient ด้วยที่อยู่และข้อมูลรับรอง.
- เริ่มต้นออบเจกต์ Contact ด้วยคุณสมบัติที่ต้องการ.
- เรียกเมธอด CreateContact เพื่อเพิ่มรายชื่อไปยัง Exchange Server.
Aspose.Email ให้ EWSClient คลาสสำหรับเชื่อมต่อกับ Microsoft Exchange Server โดยใช้ Exchange Web Services โค้ดส่วนต่อไปนี้จะแสดงวิธีต่อไปนี้ใช้ 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());
}
อัปเดตรายชื่อ
Contact information can be updated using Microsoft Outlook. Aspose.Email can also update contact information on Exchange Server using the Exchange Web Service (EWS). The IEWSClient’s 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 server โดยใช้ Exchange Web Service.
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();