การทำงานกับการกำหนดค่าผู้ใช้บนเซิร์ฟเวอร์
การจัดการการกำหนดค่าผู้ใช้
API ของ Aspose.Email สามารถใช้เพื่อจัดการการกำหนดค่าผู้ใช้บน Exchange Server ด้วย EWSClient คลาสนี้ใช้ Exchange Web Services ซึ่งมีให้เฉพาะใน Exchange Server 2007 ขึ้นไป ในบทความนี้เราจะดูวิธีอ่าน, สร้าง, ปรับปรุง, และลบการกำหนดค่าผู้ใช้บน Exchange Server 2010 จำเป็นต้องใช้ Microsoft Exchange Server 2010 Service Pack 1 สำหรับคุณสมบัติต่าง ๆ ที่อธิบายในบทความนี้
การกำหนดค่าผู้ใช้คือชุดตั้งชื่อที่แอปพลิเคชันเก็บไว้ในโฟลเดอร์ของกล่องจดหมาย แทนด้วย UserConfiguration วัตถุที่พจนานุกรมของมันเก็บการตั้งค่าเป็นคู่คีย์-ค่าและระบุด้วย UserConfigurationName - ชื่อของการกำหนดค่าและ URI ของโฟลเดอร์
ตัวอย่างในบทความนี้ใช้ส่วนหัวและเนมสเปซต่อไปนี้:
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/WebService/UserConfiguration/UserConfiguration.h>
#include <Clients/Exchange/WebService/UserConfiguration/UserConfigurationName.h>
#include <Clients/Exchange/ExchangeMailboxInfo.h>
#include <system/collections/dictionary.h>
#include <system/object_ext.h>
using namespace System;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;
ตัวอย่างทั้งหมดในบทความนี้เชื่อมต่อกับเซิร์ฟเวอร์ดังนี้:
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://your.exchange.server/ews/Exchange.asmx", u"username", u"password", u"domain");
client->Dispose();
การอ่านการกำหนดค่าผู้ใช้
เพื่อรับข้อมูลการกำหนดค่าผู้ใช้ของโฟลเดอร์เฉพาะจาก Exchange Server:
- เชื่อมต่อไปยัง Exchange Server โดยใช้ IEWSClient.
- เรียกใช้ GetUserConfiguration() เมธอดเพื่อรับการกำหนดค่าผู้ใช้ของโฟลเดอร์
- แสดงคุณสมบัติการกำหนดค่าผู้ใช้ เช่น ID, ชื่อ และรายการพจนานุกรมในรูปแบบคู่คีย์-ค่า.
คีย์และค่าของพจนานุกรมเป็นอ็อบเจกต์; สตริงที่เก็บอยู่ในนั้นเป็นแบบบ็อกซ์
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
try
{
SharedPtr<UserConfigurationName> userConfigName = MakeObject<UserConfigurationName>(u"inbox.config", client->get_MailboxInfo()->get_InboxUri());
SharedPtr<UserConfiguration> userConfig = client->GetUserConfiguration(userConfigName);
Console::WriteLine(u"Configuration Id: " + userConfig->get_Id());
Console::WriteLine(u"Configuration Name: " + userConfig->get_UserConfigurationName()->get_Name());
Console::WriteLine(u"Key value pairs:");
for (auto entry : userConfig->get_Dictionary())
{
Console::WriteLine(ObjectExt::ToString(entry.get_Key()) + u": " + ObjectExt::ToString(entry.get_Value()));
}
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
client->Dispose();
การสร้างการกำหนดค่าผู้ใช้
เพื่อสร้างการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์เฉพาะบน Exchange Server:
- เชื่อมต่อไปยัง Exchange Server โดยใช้ IEWSClient.
- สร้าง UserConfiguration พร้อมชื่อและโฟลเดอร์และเติมพจนานุกรมของมัน
- เรียกใช้ CreateUserConfiguration() เมธอดเพื่อสร้างการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<UserConfigurationName> userConfigName = MakeObject<UserConfigurationName>(u"inbox.config", client->get_MailboxInfo()->get_InboxUri());
SharedPtr<UserConfiguration> userConfig = MakeObject<UserConfiguration>(userConfigName);
userConfig->get_Dictionary()->Add(ObjectExt::Box<String>(u"key1"), ObjectExt::Box<String>(u"value1"));
userConfig->get_Dictionary()->Add(ObjectExt::Box<String>(u"key2"), ObjectExt::Box<String>(u"value2"));
userConfig->get_Dictionary()->Add(ObjectExt::Box<String>(u"key3"), ObjectExt::Box<String>(u"value3"));
try
{
client->CreateUserConfiguration(userConfig);
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
client->Dispose();
การอัปเดตการกำหนดค่าผู้ใช้
เพื่ออัปเดตการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์เฉพาะใน Exchange Server:
- เชื่อมต่อไปยัง Exchange Server โดยใช้ IEWSClient.
- อ่านการกำหนดค่าโดยใช้ GetUserConfiguration() และเปลี่ยนพจนานุกรมของมัน
- เรียกใช้ UpdateUserConfiguration() เมธอดเพื่ออัปเดตการกำหนดค่าผู้ใช้ของโฟลเดอร์
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<UserConfigurationName> userConfigName = MakeObject<UserConfigurationName>(u"inbox.config", client->get_MailboxInfo()->get_InboxUri());
SharedPtr<UserConfiguration> userConfig = client->GetUserConfiguration(userConfigName);
userConfig->get_Dictionary()->idx_set(ObjectExt::Box<String>(u"key1"), ObjectExt::Box<String>(u"new-value1"));
client->UpdateUserConfiguration(userConfig);
client->Dispose();
การลบการกำหนดค่าผู้ใช้
เพื่อลบการกำหนดค่าผู้ใช้สำหรับโฟลเดอร์เฉพาะใน Exchange Server:
- เชื่อมต่อไปยัง Exchange Server โดยใช้ IEWSClient.
- เรียกใช้ DeleteUserConfiguration() เมธอดเพื่อลบการกำหนดค่าผู้ใช้ของโฟลเดอร์
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<UserConfigurationName> userConfigName = MakeObject<UserConfigurationName>(u"inbox.config", client->get_MailboxInfo()->get_InboxUri());
try
{
client->DeleteUserConfiguration(userConfigName);
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
client->Dispose();