Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách Lưu Email Tin nhắn trong C++. Bạn sẽ quan sát cách kết nối với hộp thư của mình bằng Exchange Client để tìm nạp và lưu email từ hộp thư đó bằng các lệnh gọi API đơn giản trong C++.
Các bước để lưu thư điện tử trong C++
- Cài đặt Aspose.Email for C++ từ Công cụ quản lý gói NuGet
- Bao gồm tham chiếu đến không gian tên Aspose::Email
- Kết nối với hộp thư Exchange bằng cách cung cấp thông tin xác thực trong phương thức EWSClient.GetEWSClient()
- Lặp lại qua từng email trong ExchangeMessageInfoCollection của hộp thư
- Lưu tin nhắn ở định dạng EML và MSG trên đĩa bằng C++
Bạn có thể dễ dàng Lưu Email vào Đĩa trong C++ bằng cách sử dụng một vài dòng mã. Bạn chỉ cần kết nối với hộp thư của mình bằng EWSClient và có quyền truy cập vào bộ sưu tập thư. Sau đó, bạn có thể lưu email ở định dạng EML hoặc định dạng MSG trên đĩa bằng cách sử dụng mã đơn giản trong C++.
Mã để lưu tin nhắn email trong 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)); | |
} | |
} |
Trước đó, chúng ta đã học Cách gửi email trong C++. Trong khi đó, trong chủ đề này, chúng ta đã học cách kết nối với hộp thư bằng ứng dụng khách Exchange và lưu Thư điện tử vào đĩa trong C++.