Utility Features
This article collects several smaller features of the EWS client: sending polls, accepting server certificates that fail validation, replying to and forwarding messages on the server, adding headers to EWS requests, unified messaging and mail tips.
The samples in this article use these headers and namespaces:
#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <Attachment.h>
#include <AttachmentCollection.h>
#include <Mapi/FollowUpOptions.h>
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/WebService/UnifiedMessaging/UnifiedMessagingConfiguration.h>
#include <Clients/Exchange/WebService/MailTips/GetMailTipsOptions.h>
#include <Clients/Exchange/WebService/MailTips/MailTips.h>
#include <Clients/Exchange/WebService/MailTips/MailTipsType.h>
#include <Clients/Exchange/WebService/MailTips/OutOfOfficeMailTip.h>
#include <Clients/Exchange/WebService/MailTips/ReplyBody.h>
#include <Clients/Exchange/ExchangeMailboxInfo.h>
#include <Clients/Exchange/ExchangeMessageInfo.h>
#include <Clients/Exchange/ExchangeMessageInfoCollection.h>
#include <net/service_point_manager.h>
#include <net/secure_protocols/ssl_stream.h>
#include <system/array.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;
Sending a Message with Voting Option
Microsoft Outlook allows users to create a poll when composing a new message by adding voting buttons such as Yes, No and Maybe. The FollowUpOptions class holds these buttons in its VotingButtons property, as a semicolon-separated list.
Creating and Sending a Message with Voting Options
The overload of Send that takes a FollowUpOptions object sends the message through Exchange with the voting buttons attached, so the recipients see them in Outlook.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
String address = u"firstname.lastname@example.com";
SharedPtr<MailMessage> message = MakeObject<MailMessage>(address, address, u"Flagged message",
u"Make it nice and short, but descriptive.");
SharedPtr<FollowUpOptions> options = MakeObject<FollowUpOptions>();
options->set_VotingButtons(u"Yes;No;Maybe;Exactly!");
try
{
client->Send(message, options);
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
client->Dispose();
Ignore or Bypass Invalid or Expired SSL Certificate
When the certificate of the Exchange server has expired or cannot be validated - typical for test servers with self-signed certificates - the connection fails with an exception. A validation callback registered with System::Net::ServicePointManager decides about such certificates; a callback that always returns true accepts them. Register it once, before the first request, and only in environments you trust: it switches off the protection against impostor servers.
// Accepts every server certificate, whatever the validation result
static bool AcceptAllCertificates(SharedPtr<Object> sender,
SharedPtr<Security::Cryptography::X509Certificates::X509Certificate> certificate,
SharedPtr<Security::Cryptography::X509Certificates::X509Chain> chain,
Net::Security::SslPolicyErrors sslPolicyErrors)
{
return true;
}
Net::ServicePointManager::set_ServerCertificateValidationCallback(
Net::Security::RemoteCertificateValidationCallback(AcceptAllCertificates));
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://exchange.test.local/ews/exchange.asmx", u"username", u"password");
client->Dispose();
Creating RE and FW messages from MSG files
IEWSClient lets developers create RE (Reply/Reply All) and FW (Forward) messages from a message stored on the server. The source message is identified by an ExchangeMessageInfo from the ExchangeMessageInfoCollection returned by ListMessages(). The other argument is the MailMessage with the text and the attachments of the response. To perform this task:
- Initialize the IEWSClient object by providing valid credentials.
- Find the message to respond to.
- Call the Reply(), ReplyAll() and Forward() methods to send the responses.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://exchange.domain.com/ews/Exchange.asmx", u"username", u"password");
try
{
// a message to respond to
client->Send(MakeObject<MailMessage>(u"user@domain.com", u"user@domain.com", u"TestMailRefw", u"The original message"));
SharedPtr<ExchangeMessageInfoCollection> messages = client->ListMessages(client->get_MailboxInfo()->get_InboxUri());
// the text and the attachments of the response
SharedPtr<MailMessage> response = MakeObject<MailMessage>(u"user@domain.com", u"user@domain.com", u"TestMailRefw", u"The response");
response->get_Attachments()->Add(Attachment::CreateAttachmentFromString(u"Test attachment 1", u"Attachment Name 1"));
response->get_Attachments()->Add(Attachment::CreateAttachmentFromString(u"Test attachment 2", u"Attachment Name 2"));
client->Reply(response, messages->idx_get(0));
client->ReplyAll(response, messages->idx_get(0));
client->Forward(response, messages->idx_get(0));
}
catch (System::Exception& ex)
{
Console::WriteLine(u"Error in program: " + ex->get_Message());
}
client->Dispose();
Adding Headers in EWS Requests
Custom HTTP headers can be added to every EWS request the client sends. A common case is X-AnchorMailbox, which routes the requests of an application to the server that holds the mailbox and helps against throttling in Exchange Online. The AddHeader method of IEWSClient adds such a header.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
client->AddHeader(u"X-AnchorMailbox", u"username@domain.com");
SharedPtr<ExchangeMessageInfoCollection> messages = client->ListMessages(client->get_MailboxInfo()->get_InboxUri());
Console::WriteLine(u"Messages: " + Convert::ToString(messages->get_Count()));
client->Dispose();
Working with Unified Messaging
Exchange Server 2010 and later combine voice mail and e-mail in one mailbox - unified messaging. GetUMConfiguration returns the UnifiedMessagingConfiguration of the mailbox: whether unified messaging is enabled and the number used for playing messages on a phone. The client can also start an outbound call, read the information about a call by its ID and disconnect it.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://exchange.domain.com/ews/Exchange.asmx", u"username", u"password");
SharedPtr<UnifiedMessagingConfiguration> configuration = client->GetUMConfiguration();
Console::WriteLine(u"Unified messaging enabled: " + Convert::ToString(configuration->get_UMEnabled()));
Console::WriteLine(u"Play on phone: " + Convert::ToString(configuration->get_PlayOnPhoneEnabled()) +
u", dial string: " + configuration->get_PlayOnPhoneDialString());
client->Dispose();
Getting Mail Tips
Exchange Server 2010 and later can give mail tips while a message is being composed - information about the recipients before the message is sent. A tip reports, for example, that a recipient address is invalid or that a recipient has set an out-of-office reply. Exchange Server 2010 Service Pack 1 or later is required.
GetMailTipsOptions names the sender, the recipients and the kinds of tips to request (MailTipsType); GetMailTips returns one MailTips object per recipient. The second argument of the MailAddress constructors below, true, skips the syntax check of the addresses.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://exchange.domain.com/ews/Exchange.asmx", u"username", u"password");
SharedPtr<MailAddressCollection> recipients = MakeObject<MailAddressCollection>();
recipients->Add(MakeObject<MailAddress>(u"test.exchange@ex2010.local", true));
recipients->Add(MakeObject<MailAddress>(u"invalid.recipient@ex2010.local", true));
SharedPtr<GetMailTipsOptions> options = MakeObject<GetMailTipsOptions>(
MakeObject<MailAddress>(u"administrator@ex2010.local", true), recipients, MailTipsType::All);
try
{
for (SharedPtr<MailTips> tip : client->GetMailTips(options))
{
if (tip->get_OutOfOffice() != nullptr)
{
Console::WriteLine(u"Out of office: " + tip->get_OutOfOffice()->get_ReplyBody()->get_Message());
}
if (tip->get_InvalidRecipient())
{
Console::WriteLine(u"The recipient address is invalid: " + tip->get_RecipientAddress()->get_Address());
}
}
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
client->Dispose();