คุณสมบัติอรรถประโยชน์
บทความนี้รวบรวมฟีเจอร์ย่อยหลายอย่างของไคลเอนต์ 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 อนุญาตให้ผู้ใช้สร้างแบบสำรวจเมื่อเขียนข้อความใหม่โดยเพิ่มปุ่มโหวตเช่น ใช่, ไม่และอาจจะ The 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) จากข้อความที่เก็บบนเซิร์ฟเวอร์ ข้อความต้นทางระบุด้วย 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, ซึ่งทำให้คำขอของแอปพลิเคชันไปยังเซิร์ฟเวอร์ที่เก็บกล่องจดหมายและช่วยลด throttling ใน 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 และต่อมาผสานวอยซ์เมลและอีเมลในกล่องเดียว - Unified Messaging GetUMConfiguration คืนค่า UnifiedMessagingConfiguration ของกล่องจดหมาย: ว่า Unified Messaging เปิดหรือไม่และหมายเลขที่ใช้เล่นข้อความบนโทรศัพท์ ลูกค้าสามารถเริ่มการโทรออก, อ่านข้อมูลการโทรด้วย 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();