การทำงานกับการกำหนดค่าผู้ใช้บนเซิร์ฟเวอร์

การจัดการการกำหนดค่าผู้ใช้

Aspose.Email for Java สามารถใช้เพื่อจัดการการกำหนดค่าผู้ใช้บน Exchange Server ด้วย EWSClient คลาส นี้ใช้ Exchange Web Services ซึ่งมีเฉพาะใน Exchange Server 2007 ขึ้นไป ในบทความนี้ เราจะดูวิธีการอ่าน, สร้าง, อัปเดต และลบการกำหนดค่าผู้ค้าบน Exchange Server 2010 จำเป็นต้องมี Microsoft Exchange Server 2010 Service Pack 1 สำหรับคุณลักษณะทั้งหมดที่อธิบายในบทความนี้ โค้ดตัวอย่างต่อไปนี้แสดงวิธีเชื่อมต่อกับ Exchange Server 2010 ในตัวอย่างทั้งหมดของบทความนี้.

private static IEWSClient getExchangeEWSClient() {
    final String mailboxUri = "https://outlook.office365.com/ews/exchange.asmx";
    final String domain = "";
    final String username = "username@ASE305.onmicrosoft.com";
    final String password = "password";
    NetworkCredential credentials = new NetworkCredential(username, password, domain);
    IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
    return client;
}

การอ่านการกำหนดค่าผู้ใช้

เพื่อรับข้อมูลการกำหนดค่าผู้ใช้ของโฟลเดอร์เฉพาะจาก Exchange Server:

  1. เชื่อมต่อกับ Exchange Server โดยใช้คลาส IEWSClient.
  2. เรียกเมธอด IEWSClient.getUserConfiguration() เพื่อรับการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์.
  3. แสดงคุณสมบัติการกำหนดค่าผู้ใช้ เช่น ID, ชื่อ และรายการพจนานุกรมในรูปแบบคู่คีย์-ค่า.

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการอ่านการกำหนดค่าผู้ใช้.

IEWSClient client = getExchangeEWSClient();
System.out.println("Connected to Exchange 2010");

// Get the User Configuration for Inbox folder
UserConfigurationName userConfigName = new UserConfigurationName("inbox.config", client.getMailboxInfo().getInboxUri());
UserConfiguration userConfig = client.getUserConfiguration(userConfigName);

System.out.println("Configuration Id: " + userConfig.getId());
System.out.println("Configuration Name: " + userConfig.getUserConfigurationName().getName());
System.out.println("Key value pairs:");
// foreach to while statements conversion
for (Object key : userConfig.getDictionary().keySet()) {
    System.out.println(key + ": " + userConfig.getDictionary().get(key).toString());
}

การสร้างการกำหนดค่าผู้ใช้

เพื่อสร้างการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์เฉพาะบน Exchange Server:

  1. เชื่อมต่อกับ Exchange Server โดยใช้คลาส IEWSClient.
  2. เรียกเมธอด IEWSClient.createUserConfiguration() เพื่อสร้างการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์.

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการสร้างการกำหนดค่าผู้ใช้.

IEWSClient client = getExchangeEWSClient();
System.out.println("Connected to Exchange 2010");

// Create the User Configuration for Inbox folder
UserConfigurationName userConfigName = new UserConfigurationName("inbox.config", client.getMailboxInfo().getInboxUri());
UserConfiguration userConfig = new UserConfiguration(userConfigName);
userConfig.getDictionary().put("key1", "value1");
userConfig.getDictionary().put("key2", "value2");
userConfig.getDictionary().put("key3", "value3");
client.createUserConfiguration(userConfig);

การอัปเดตการกำหนดค่าผู้ใช้

เพื่ออัปเดตการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์เฉพาะใน Exchange Server:

  1. เชื่อมต่อกับ Exchange Server โดยใช้คลาส IEWSClient.
  2. เรียกเมธอด IEWSClient.updateUserConfiguration() เพื่ออัปเดตการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์.

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการอัปเดตการกำหนดค่าผู้ใช้.

IEWSClient client = getExchangeEWSClient();
System.out.println("Connected to Exchange 2010");

// Create the User Configuration for Inbox folder
UserConfigurationName userConfigName = new UserConfigurationName("inbox.config", client.getMailboxInfo().getInboxUri());
UserConfiguration userConfig = client.getUserConfiguration(userConfigName);
userConfig.setId(null);

// Update User Configuration
userConfig.getDictionary().put("key1", "new-value1");
client.updateUserConfiguration(userConfig);

การลบการกำหนดค่าผู้ใช้

เพื่อลบการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์เฉพาะใน Exchange Server:

  1. เชื่อมต่อกับ Exchange Server โดยใช้คลาส IEWSClient.
  2. เรียกเมธอด IEWSClient.deleteUserConfiguration() เพื่อลบการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์.

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการลบการกำหนดค่าผู้ใช้.

IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
System.out.println("Connected to Exchange 2010");

// Delete User Configuration
UserConfigurationName userConfigName = new UserConfigurationName("inbox.config", client.getMailboxInfo().getInboxUri());
client.deleteUserConfiguration(userConfigName);