Exchange Web Services (EWS)로 연락처 관리

Aspose.Email를 사용하면 Microsoft Exchange Server에서 이메일 메시지뿐만 아니라 다양한 작업을 수행할 수 있습니다. 사용하여 EWSClient API를 사용하면 Exchange 사서함에 저장된 연락처를 검색, 확인, 생성, 업데이트 및 삭제할 수 있습니다. 이 문서에서는 Exchange Web Services (EWS)를 사용한 연락처 관리의 핵심 작업을 설명합니다.

클라이언트는 연락처를 Contact 객체. 이 기사에 있는 샘플은 다음 헤더와 네임스페이스를 사용합니다:

#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로 연락처 가져오기

GetContacts 폴더의 모든 연락처를 읽습니다; 기본 연락처 폴더의 URI는 사서함 정보에서 가져옵니다. 아래 예제는 각 연락처의 이름과 첫 번째 이메일 주소를 표시합니다.

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

이름으로 연락처 해결

ResolveContacts 이름을 기준으로 연락처와 디렉터리를 검색합니다. Outlook이 받는 사람 필드에 입력한 이름을 해석하는 방식과 동일합니다. ExchangeListContactsOptions::FetchPhoto 연락처 사진도 다운로드합니다.

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

연락처 메모 형식 결정

다음은 Contact->get_NotesFormat 속성은 메모 텍스트의 형식을 다음에 따라 지정합니다. TextFormat 열거자.

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

ID로 연락처 가져오기

서버에 저장된 모든 연락처는 ID를 가집니다 - get_Id 그 ID를 반환하고, CreateContact 새 연락처의 ID를 반환합니다. GetContact 해당 ID로 단일 연락처를 읽습니다.

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

연락처 추가

다음 사용 CreateContact() Exchange Server에 새 연락처를 추가하는 메서드. 전화번호, 관련 인물, 웹 주소 및 이메일 주소는 연락처의 목록에 저장되며, 각 항목은 카테고리(예: 집 전화, 배우자, 블로그, 첫 번째 이메일 주소 등)를 가집니다. ...Category 클래스.

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

연락처 업데이트

연락처 정보는 서버에서 다음을 사용하여 수정할 수 있습니다 IEWSClient->UpdateContact: 서버에서 읽은 연락처 속성을 변경하고 다시 전달합니다. 다음 코드 샘플은 연락처 폴더의 첫 번째 연락처 이름을 변경합니다.

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

연락처 삭제

연락처는 사서함의 다른 항목과 같이 삭제됩니다: DeleteItem 그 ID와 DeletionOptions - get_DeletePermanently() 항목을 제거합니다, get_MoveToDeletedItems() 삭제된 항목 폴더로 이동합니다.

다음 코드 조각은 지정된 표시 이름을 가진 연락처를 삭제합니다.

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