实用功能
本文收集了 EWS 客户端的几个小功能:发送投票、接受验证失败的服务器证书、在服务器上回复和转发邮件、向 EWS 请求添加标头、统一消息和邮件提示。
本文中的示例使用了以下头文件和名称空间:
#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;
发送带投票选项的消息
Microsoft Outlook 允许用户在撰写新邮件时通过添加“是”“否”“可能”等投票按钮来创建投票。该 FollowUpOptions 类在其 VotingButtons 属性,使用分号分隔的列表。
创建并发送带投票选项的消息
的重载 发送 它接受一个 FollowUpOptions 对象通过 Exchange 发送带有投票按钮的消息,因此收件人在 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();
忽略或绕过无效或已过期的 SSL 证书
当 Exchange 服务器的证书已过期或无法验证时——这在使用自签名证书的测试服务器中很常见——连接会因异常而失败。使用以下方式注册的验证回调函数 System::Net::ServicePointManager 决定此类证书的事项;一个始终返回的回调函数 true 接受它们。请在首次请求之前注册一次,仅在受信任的环境中使用:它会关闭针对冒充服务器的保护。
// 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();
从 MSG 文件创建 RE 和 FW 消息
IEWSClient 允许开发人员从服务器上存储的消息创建 RE(回复/全部回复)和 FW(转发)消息。源消息由一个 ExchangeMessageInfo 自 ExchangeMessageInfoCollection 由 ListMessages()。另一个参数是 MailMessage 使用响应的文本和附件。执行此任务:
- 初始化 IEWSClient 通过提供有效凭据来获取对象。
- 查找要响应的消息。
- 调用 Reply(), ReplyAll() 和 Forward() 方法用于发送响应。
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();
在 EWS 请求中添加标头
自定义 HTTP 标头可以添加到客户端发送的每个 EWS 请求中。常见的情况是 X-AnchorMailbox,它将应用程序的请求路由到持有邮箱的服务器,并有助于防止 Exchange Online 的限流。该 AddHeader 方法的 IEWSClient 添加此类标题。
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();
使用统一消息
Exchange Server 2010 及以后版本将语音邮件和电子邮件合并到同一邮箱——统一消息。 GetUMConfiguration 返回 UnifiedMessagingConfiguration 关于邮箱的配置:是否启用统一消息以及用于在电话上播放消息的号码。客户端还可以发起呼叫、通过 ID 读取通话信息并挂断。
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();
获取邮件提示
Exchange Server 2010 及以后版本在撰写邮件时可以提供 邮件提示——在发送前提供收件人信息。例如,提示收件人地址无效或收件人已设置外出自动回复。需要 Exchange Server 2010 Service Pack 1 或更高版本。
GetMailTipsOptions 指定发件人、收件人以及要请求的提示类型(MailTipsType); GetMailTips 返回一个 MailTips 每个收件人的对象。该 MailAddress 下面的构造函数, true,跳过地址的语法检查。
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();