תכונות שימושיות
מאמר זה אוסף מספר תכונות קטנות של לקוח 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 (Reply/Reply All) ו‑FW (Forward) מהודעה שמורה על ה‑server. הודעת המקור מזוהה על‑ידי 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 שה‑client שולח. מקרה נפוץ הוא 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 ומעלה משלב משא voicemail ו‑email בתיבת דואר אחת – הודעות מאוחדות. GetUMConfiguration מחזיר את ה‑ UnifiedMessagingConfiguration של תיבת הדואר: האם הודעות מאוחדות מופעלות והמספר המשמש לשמירת הודעות בטלפון. ה‑client יכול גם להתחיל שיחה יוצאת, לקרוא מידע על שיחה לפי ה‑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 ומעלה יכולים להציג mail tips בזמן שהודעה נערכת – מידע על הנמענים לפני שליחת ההודעה. טיפ מדווח, לדוגמה, שהכתובת של נמען אינה תקפה או שנמען הגדר תשובה מחוץ למשרד. נדרש 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();