Verktygsfunktioner
Denna artikel samlar flera mindre funktioner i EWS‑klienten: skicka omröstningar, acceptera servercertifikat som misslyckas med validering, svara på och vidarebefordra meddelanden på servern, lägga till rubriker till EWS‑förfrågningar, enhetlig meddelandehantering och e‑posttips.
Exemplen i denna artikel använder dessa rubriker och namnrymder:
#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;
Skicka ett meddelande med röstningsalternativ
Microsoft Outlook låter användare skapa en omröstning när de skriver ett nytt meddelande genom att lägga till röstningsknappar som Ja, Nej och Kanske. Den FollowUpOptions klassen håller dessa knappar i sin VotingButtons egenskap, som en semikolonseparerad lista.
Skapa och skicka ett meddelande med röstningsalternativ
Överlagringen av Skicka som tar en FollowUpOptions objektet skickar meddelandet via Exchange med röstningsknapparna bifogade, så mottagarna ser dem i 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();
Ignorera eller kringgå ogiltigt eller utgånget SSL‑certifikat
När certifikatet för Exchange‑servern har gått ut eller inte kan valideras – typiskt för testservrar med självsignerade certifikat – misslyckas anslutningen med ett undantag. Ett valideringsåteruppringningsfunktion registrerad med System::Net::ServicePointManager bestämmer sådana certifikat; ett återuppringningsfunktion som alltid returnerar true accepterar dem. Registrera den en gång, före första förfrågan, och bara i miljöer du litar på: den stänger av skyddet mot impersonations‑servrar.
// 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();
Skapa RE‑ och FW‑meddelanden från MSG‑filer
IEWSClient låter utvecklare skapa RE (Reply/Reply All) och FW (Forward) meddelanden från ett meddelande lagrat på servern. Källmeddelandet identifieras av en ExchangeMessageInfo från ExchangeMessageInfoCollection returnerad av ListMessages(). Det andra argumentet är MailMessage med svarstexten och bifogade filer. För att utföra detta:
- Initiera IEWSClient objekt genom att ange giltiga autentiseringsuppgifter.
- Hitta meddelandet att svara på.
- Anropa Reply(), ReplyAll() och Forward() metoder för att skicka svaren.
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();
Lägga till headers i EWS‑förfrågningar
Anpassade HTTP‑rubriker kan läggas till i varje EWS‑förfrågan som klienten skickar. Ett vanligt fall är X-AnchorMailbox, som dirigerar en applikations förfrågningar till den server som har brevlådan och hjälper mot belastningsbegränsning i Exchange Online. AddHeader metod för IEWSClient lägger till ett sådant rubrik.
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();
Arbeta med Unified Messaging
Exchange Server 2010 och senare kombinerar röstbrevlåda och e‑post i en brevlåda – unified messaging. GetUMConfiguration returnerar UnifiedMessagingConfiguration för brevlådan: om unified messaging är aktiverat och vilket nummer som används för att spela upp meddelanden på en telefon. Klienten kan också initiera ett utgående samtal, läsa information om ett samtal via dess ID och avsluta det.
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();
Hämta mejltips
Exchange Server 2010 och senare kan ge mail‑tips medan ett meddelande komponeras – information om mottagarna innan meddelandet skickas. Ett tips rapporterar exempelvis att en mottagareadress är ogiltig eller att en mottagare har ställt in ett autosvarsmeddelande. Exchange Server 2010 Service Pack 1 eller senare krävs.
GetMailTipsOptions namnger avsändaren, mottagarna och vilka slags tips som ska begäras (MailTipsType); GetMailTips returnerar en MailTips objekt per mottagare. Det andra argumentet i MailAddress konstruktörerna nedan, true, hoppar över syntaxkontrollen av adresserna.
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();