在本教程中,我们将学习如何在 C++ 中保存 Email 消息。您将观察如何使用 Exchange 客户端连接您的邮箱,以使用 C++ 中的简单 API 调用从中获取和保存电子邮件。
在 C++ 中保存电子邮件的步骤
- 从 NuGet 包管理器工具安装 Aspose.Email for C++
- 包括对 Aspose::Email 命名空间的引用
- 通过在 EWSClient.GetEWSClient() 方法中提供凭据连接到 Exchange 邮箱
- 遍历邮箱的 ExchangeMessageInfoCollection 中的每封邮件
- 使用 C++ 将消息以 EML 和 MSG 格式保存在磁盘上
您可以使用几行代码轻松地将电子邮件保存到 C++ 中的磁盘。您只需要使用 EWSClient 连接到您的邮箱并获得对消息集合的访问权限。然后,您可以使用 C++ 中的简单代码以 EML 格式或 MSG 格式将电子邮件保存在磁盘上。
用 C++ 保存电子邮件的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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++ 中的磁盘。