このチュートリアルでは、EmailメッセージをC++で保存する方法を学習します。 Exchangeクライアントを使用してメールボックスに接続し、C++の単純なAPI呼び出しを使用してメールボックスから電子メールをフェッチおよび保存する方法を確認します。
電子メールメッセージをC++で保存する手順
- NuGetパッケージマネージャーツールからAspose.Email for C++をインストールします
- Aspose::Email名前空間への参照を含める
- EWSClient.GetEWSClient()メソッドで資格情報を提供してExchangeメールボックスに接続します
- メールボックスのExchangeMessageInfoCollection内の各電子メールを反復処理します
- C++を使用してメッセージをEMLおよびMSG形式でディスクに保存します
数行のコードを使用して、簡単にメールをC++でディスクに保存できます。 EWSClientを使用してメールボックスに接続し、メッセージコレクションにアクセスする必要があります。次に、C ++の単純なコードを使用して、電子メールメッセージをEML形式またはMSG形式でディスクに保存できます。
電子メールメッセージをC++で保存するコード
#pragma once | |
#include <Tools/Search/StringComparisonField.h> | |
#include <Tools/Search/MailQuery.h> | |
#include <Tools/Search/BoolComparisonField.h> | |
#include <system/string.h> | |
#include <system/shared_ptr.h> | |
#include <stdio.h> | |
#include <system/object.h> | |
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h> | |
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h> | |
#include <MailAddress.h> | |
#include <MailAddressCollection.h> | |
#include <MailAddress.h> | |
#include <Clients/Exchange/ExchangeMessageInfoCollection.h> | |
#include <Clients/Exchange/ExchangeMailboxInfo.h> | |
#include <system/console.h> | |
#include <system/environment.h> | |
#include <system/object_ext.h> | |
#include <Licensing/License.h> | |
using namespace Aspose::Email; | |
using namespace Aspose::Email::Clients::Exchange; | |
using namespace Aspose::Email::Clients::Exchange::WebService; | |
using namespace System; | |
void SaveEmailFromEWS() | |
{ | |
// Set the license for Aspose.Email for CPP | |
SharedPtr<License> license = System::MakeObject<License>(); | |
license->SetLicense(u"licFile"); | |
// EWS Client Credentials | |
const System::String mailboxUri = u"https://outlook.office365.com/ews/exchange.asmx"; | |
const System::String username = u"username"; | |
const System::String password = u"password"; | |
const System::String domain = u"domain"; | |
try | |
{ | |
// Connect to EWS | |
System::SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(mailboxUri, username, password, domain); | |
// Get mailbox | |
System::SharedPtr<ExchangeMailboxInfo> MailBoxInfo = client->GetMailboxInfo(); | |
// Get list of all messages from the mail box | |
System::SharedPtr< ExchangeMessageInfoCollection> MessagesCollection = | |
client->ListMessages(client->GetMailboxInfo()->get_InboxUri()); | |
//Loop through email messages | |
int MessageCount = 1; | |
for(System::SharedPtr <ExchangeMessageInfo> MessageInfo : MessagesCollection) | |
{ | |
System::String strMessageURI = MessageInfo->get_UniqueUri(); | |
// Access message details | |
System::Console::Write(System::Environment::get_NewLine() + | |
System::ObjectExt::ToString(u"Subject: " + MessageInfo->get_Subject())); | |
System::Console::Write(System::Environment::get_NewLine() + | |
System::ObjectExt::ToString(u"From: " + System::ObjectExt::ToString(MessageInfo->get_From()))); | |
System::Console::Write(System::Environment::get_NewLine() + | |
System::ObjectExt::ToString(u"To: " + System::ObjectExt::ToString(MessageInfo->get_To()))); | |
System::Console::Write(System::Environment::get_NewLine() + | |
System::ObjectExt::ToString(u"Message ID: " + MessageInfo->get_MessageId())); | |
System::Console::Write(System::Environment::get_NewLine() + | |
System::ObjectExt::ToString(u"Unique URI: " + MessageInfo->get_UniqueUri())); | |
// Save each message on disk in EML and MSG formats | |
System::String MessageStr = u"Messsage_"+MessageCount; | |
client->SaveMessage(strMessageURI, MessageStr + u".eml"); | |
client->SaveMessage(strMessageURI, MessageStr + u".msg"); | |
MessageCount++; | |
} | |
} | |
catch (System::Exception ex) | |
{ | |
System::Console::Write(System::Environment::get_NewLine() + System::ObjectExt::ToString(ex)); | |
} | |
} |
以前、C++でメールを送信する方法を学びました。一方、このトピックでは、Exchangeクライアントを使用してメールボックスに接続し、*電子メールメッセージをC++*でディスクに保存する方法を学習しました。