Tính năng tiện ích

Bài viết này tổng hợp một số tính năng nhỏ của client EWS: gửi thăm dò, chấp nhận chứng chỉ máy chủ không qua xác thực, trả lời và chuyển tiếp tin nhắn trên máy chủ, thêm header vào yêu cầu EWS, nhắn tin thống nhất và mail tips.

Các mẫu trong bài viết này sử dụng các tiêu đề và không gian tên sau:

#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;

Gửi tin với tùy chọn Bình chọn

Microsoft Outlook cho phép người dùng tạo một cuộc thăm dò khi soạn tin mới bằng cách thêm các nút bỏ phiếu như Có, Không và Có thể. The FollowUpOptions lớp chứa các nút này trong VotingButtons thuộc tính, dưới dạng danh sách phân tách bằng dấu chấm phẩy.

Tạo và Gửi tin với tùy chọn Bình chọn

Phiên bản overload của Gửi mà nhận một FollowUpOptions đối tượng gửi tin nhắn qua Exchange kèm các nút bỏ phiếu, vì vậy người nhận sẽ thấy chúng trong 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();

Bỏ qua hoặc Vượt qua Chứng chỉ SSL Không hợp lệ hoặc Đã hết hạn

Khi chứng chỉ của máy chủ Exchange đã hết hạn hoặc không thể xác thực - thường gặp trên các máy chủ thử nghiệm với chứng chỉ tự ký - kết nối sẽ thất bại với một ngoại lệ. Một callback xác thực được đăng ký với System::Net::ServicePointManager quyết định về các chứng chỉ như vậy; một callback luôn trả về true chấp nhận chúng. Đăng ký một lần, trước lời gọi đầu tiên, và chỉ trong môi trường bạn tin tưởng: nó tắt bảo vệ chống lại máy chủ giả.

// 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();

Tạo tin RE và FW từ tệp MSG

IEWSClient cho phép các nhà phát triển tạo tin RE (Reply/Reply All) và FW (Forward) từ một tin được lưu trên máy chủ. Tin gốc được xác định bằng một ExchangeMessageInfo từ ExchangeMessageInfoCollection được trả về bởi ListMessages(). Đối số khác là MailMessage với nội dung và tệp đính kèm của phản hồi. Để thực hiện tác vụ này:

  1. Khởi tạo IEWSClient đối tượng bằng cách cung cấp thông tin xác thực hợp lệ.
  2. Tìm tin nhắn cần trả lời.
  3. Gọi Reply(), ReplyAll() và Forward() phương pháp để gửi các phản hồi.
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();

Thêm Header trong Yêu cầu EWS

Các tiêu đề HTTP tùy chỉnh có thể được thêm vào mọi yêu cầu EWS mà client gửi. Một trường hợp phổ biến là X-AnchorMailbox, định tuyến các yêu cầu của ứng dụng tới máy chủ chứa hộp thư và giúp giảm throttling trong Exchange Online. The AddHeader phương thức của IEWSClient thêm một tiêu đề như vậy.

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();

Làm việc với Nhắn tin Thống nhất

Exchange Server 2010 và các phiên bản sau kết hợp thư thoại và email trong một hộp thư - nhắn tin thống hợp. GetUMConfiguration trả về UnifiedMessagingConfiguration của hộp thư: liệu nhắn tin thống hợp có được bật hay không và số dùng để phát tin nhắn trên điện thoại. Client cũng có thể bắt đầu một cuộc gọi đi, đọc thông tin về cuộc gọi bằng ID và ngắt kết nối.

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();

Nhận Lời khuyên Email

Exchange Server 2010 và các phiên bản sau có thể cung cấp mail tips khi đang soạn tin - thông tin về người nhận trước khi tin được gửi. Một mẹo có thể báo, ví dụ, địa chỉ người nhận không hợp lệ hoặc người nhận đã thiết lập trả lời ngoài giờ. Yêu cầu Exchange Server 2010 Service Pack 1 trở lên.

GetMailTipsOptions đặt tên cho người gửi, người nhận và các loại mẹo cần yêu cầu (MailTipsType); GetMailTips trả về một MailTips đối tượng cho mỗi người nhận. Đối số thứ hai của MailAddress các hàm khởi tạo dưới đây, true, bỏ qua kiểm tra cú pháp của các địa chỉ.

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();