In this tutorial, we will learn how to Save Email Message in C++. You will observe how to connect with your mailbox using Exchange Client to fetch and save the emails from it using simple API calls in C++.
Steps to Save Email Message in C++
- Install Aspose.Email for C++ from NuGet package Manager Tool
- Include reference to Aspose::Email namespace
- Connect to Exchange mailbox by providing credentials in EWSClient.GetEWSClient() method
- Iterate through each email in ExchangeMessageInfoCollection of mailbox
- Save the message in EML and MSG format on disk using C++
You can easily Save Emails to Disk in C++ by using few lines of code. You just need to connect to your mailbox using EWSClient and get access to message collection. Then you can save the email messages either in EML format or MSG format on disk using simple code in C++.
Code to Save Email Message in 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)); | |
} | |
} |
Earlier, we learnt How to Send Email in C++. Whereas, in this topic we have learnt how to connect to mailbox using Exchange client and save Email Message to disk in C++.