Connecting to Microsoft Exchange via EWS
In order to connect to Exchange servers and MS 365 using Exchange Web Service, Aspose.Email provides the EWSClient class. The GetEWSClient method instantiates and returns an IEWSClient object that is further used to perform operations related to an Exchange mailbox and other folders. This article shows how to instantiate objects of IEWSClient.
The samples in this article use these headers and namespaces:
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/ExchangeMailboxInfo.h>
#include <net/network_credential.h>
using namespace System;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;
Connect with a User Name and a Password
GetEWSClient takes the URL of the EWS endpoint of the server - for Microsoft 365 it is https://outlook.office365.com/ews/exchange.asmx - together with the credentials of the mailbox. The domain argument is needed only when the account belongs to a Windows domain. The returned client keeps the connection settings and is used for every further request; Dispose releases it when the work is done.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(
u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password", u"domain");
// the first request to the server: the URIs of the standard folders of the mailbox
Console::WriteLine(u"Inbox: " + client->get_MailboxInfo()->get_InboxUri());
client->Dispose();
Connect with Network Credentials
The credentials can also be passed as an object. A System::Net::NetworkCredential holds the user name, the password and the domain, and the overload of GetEWSClient that takes an ICredentials accepts it. This form is useful when the credentials come from elsewhere in the application.
String mailboxUri = u"https://your.exchange.server/ews/Exchange.asmx";
SharedPtr<Net::NetworkCredential> credentials = MakeObject<Net::NetworkCredential>(u"username", u"password", u"domain");
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(mailboxUri, credentials);
Console::WriteLine(u"Connected to " + mailboxUri);
client->Dispose();