Exchange Web Services (EWS) ile Kişileri Yönetme
Aspose.Email, Microsoft Exchange Server’da yalnızca e-posta mesajlarıyla sınırlı kalmadan çalışmanıza olanak tanır. Kullanarak EWSClient API ile Exchange posta kutusunda saklanan kişileri alabilir, çözümleyebilir, oluşturabilir, güncelleyebilir ve silebilirsiniz. Bu makale, Exchange Web Services (EWS) ile kişileri yönetmek için temel işlemleri açıklar.
İstemci bir kişiyi bir Contact nesne. Bu makaledeki örnekler bu üstbilgileri ve ad alanlarını kullanır:
#include <ObjectIdentifier.h>
#include <PersonalInfo/Contact.h>
#include <PersonalInfo/Gender.h>
#include <PersonalInfo/TextFormat.h>
#include <PersonalInfo/EmailAddress.h>
#include <PersonalInfo/EmailAddressList.h>
#include <PersonalInfo/EmailAddressCategory.h>
#include <PersonalInfo/PhoneNumber.h>
#include <PersonalInfo/PhoneNumberList.h>
#include <PersonalInfo/PhoneNumberCategory.h>
#include <PersonalInfo/AssociatedPerson.h>
#include <PersonalInfo/AssociatedPersonsList.h>
#include <PersonalInfo/AssociatedPersonCategory.h>
#include <PersonalInfo/Url.h>
#include <PersonalInfo/URLList.h>
#include <PersonalInfo/UrlCategory.h>
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/DeletionOptions.h>
#include <Clients/Exchange/WebService/ExchangeListContactsOptions.h>
#include <Clients/Exchange/ExchangeMailboxInfo.h>
#include <system/array.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::PersonalInfo;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;
EWS ile Kişileri Al
GetContacts bir klasörün tüm kişilerini okur; varsayılan Kişiler klasörünün URI’sı posta kutusu bilgilerinden gelir. Aşağıdaki örnek, her kişinin adını ve birinci e-posta adresini gösterir.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
for (SharedPtr<Contact> contact : contacts)
{
SharedPtr<EmailAddress> email = contact->get_EmailAddresses()->get_Email1();
Console::WriteLine(u"Name: " + contact->get_DisplayName() + u", Email Address: " + (email != nullptr ? email->ToString() : String(u"-")));
}
client->Dispose();
İsme Göre Kişileri Çözümle
ResolveContacts kişileri ve dizini, Outlook’un To alanına yazılan adı çözdüğü şekilde arar. ExchangeListContactsOptions::FetchPhoto aynı zamanda kişi fotoğraflarını da indirir.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->ResolveContacts(u"Changed Name", ExchangeListContactsOptions::FetchPhoto);
for (SharedPtr<Contact> contact : contacts)
{
Console::WriteLine(u"Name: " + contact->get_DisplayName());
}
client->Dispose();
Kişi Notları Biçimini Belirle
Bu Contact->get_NotesFormat özellik, not metninin formatını belirtir TextFormat enumerator.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
for (SharedPtr<Contact> contact : client->GetContacts(client->get_MailboxInfo()->get_ContactsUri()))
{
Console::WriteLine(contact->get_DisplayName() + String(u": notes are ") + (contact->get_NotesFormat() == TextFormat::Html ? u"HTML" : u"plain text"));
}
client->Dispose();
Kimlik Kullanarak Kişi Getirme
Sunucuda depolanan her kişinin bir ID’si vardır - get_Id onu döndürür ve CreateContact yeni bir kişinin ID’sini döndürür. GetContact bu ID ile tek bir kişiyi okur.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
String id = contacts[0]->get_Id()->get_EWSId();
SharedPtr<Contact> fetchedContact = client->GetContact(id);
Console::WriteLine(u"Fetched: " + fetchedContact->get_DisplayName());
client->Dispose();
Kişi Ekle
Kullan CreateContact() Exchange Server’a yeni bir kişi eklemek için yöntem. Telefon numaraları, ilişkili kişiler, web adresleri ve e-posta adresleri kişi üzerinde listeler halinde tutulur; her giriş bir kategoriye sahiptir - ev telefonu, eş, blog, birinci e-posta adresi vb. - eşleşen ...Category sınıf.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<Contact> contact = MakeObject<Contact>();
contact->set_Gender(Gender::Male);
contact->set_DisplayName(u"Frank Lin");
contact->set_CompanyName(u"ABC Co.");
contact->set_JobTitle(u"Executive Manager");
SharedPtr<PhoneNumber> phone = MakeObject<PhoneNumber>();
phone->set_Number(u"123456789");
phone->set_Category(PhoneNumberCategory::get_Home());
contact->get_PhoneNumbers()->Add(phone);
SharedPtr<AssociatedPerson> spouse = MakeObject<AssociatedPerson>();
spouse->set_Name(u"Catherine");
spouse->set_Category(AssociatedPersonCategory::get_Spouse());
contact->get_AssociatedPersons()->Add(spouse);
SharedPtr<Url> blog = MakeObject<Url>();
blog->set_Href(u"www.blog.com");
blog->set_Category(UrlCategory::get_Blog());
contact->get_Urls()->Add(blog);
SharedPtr<EmailAddress> email = MakeObject<EmailAddress>(u"Frank.Lin@Abc.com", String(u"Frank Lin"));
email->set_Category(EmailAddressCategory::get_Email1());
contact->get_EmailAddresses()->Add(email);
try
{
String id = client->CreateContact(contact);
Console::WriteLine(u"Contact created: " + id);
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
client->Dispose();
Kişileri Güncelle
Sunucuda kişi bilgileri aşağıdakini kullanarak değiştirilebilir IEWSClient->UpdateContact: sunucudan okunan bir kişi özelliğini değiştir ve geri gönder. Aşağıdaki kod örneği, Kişiler klasöründeki ilk kişiyi yeniden adlandırır.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
SharedPtr<Contact> contact = contacts[0];
Console::WriteLine(u"Name: " + contact->get_DisplayName());
contact->set_DisplayName(u"David Ch");
client->UpdateContact(contact);
client->Dispose();
Kişileri Silme
Bir kişi, posta kutusundaki diğer herhangi bir öğe gibi silinir: DeleteItem ID’sini ve DeletionOptions - get_DeletePermanently() öğeyi kaldırır, get_MoveToDeletedItems() Silinmiş Öğeler klasörüne taşır.
Aşağıdaki kod parçacığı, verilen görünen adla kişileri siler.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
String contactToDelete = u"John Teddy";
for (SharedPtr<Contact> contact : client->GetContacts(client->get_MailboxInfo()->get_ContactsUri()))
{
if (contact->get_DisplayName() == contactToDelete)
{
client->DeleteItem(contact->get_Id()->get_EWSId(), DeletionOptions::get_DeletePermanently());
}
}
client->Dispose();