ميزات الأدوات المساعدة
تجمع هذه المقالة عدة ميزات أصغر لعميل 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();
إنشاء رسائل RE و FW من ملفات MSG
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 من علبة البريد: ما إذا كان الرسائل الموحدة مفعلة والرقم المستخدم لتشغيل الرسائل على هاتف. يمكن للعميل أيضًا بدء مكالمة صادرة، قراءة معلومات عن مكالمة عبر معرّفها وإنهائها.
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();