Working with IBM Notes (NSF) Files
IBM Notes (now HCL Notes) keeps mail, calendar entries and application data in NSF databases - the Notes Storage Facility format. Aspose.Email for C++ can recognize such a database and pull the mail messages out of it through the NotesStorageFacility class of the Aspose::Email::Storage::Nsf namespace. No Notes client or Domino server is required.
The samples below use the following headers and namespaces:
#include <MailMessage.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <Storage/Nsf/NotesStorageFacility.h>
#include <Tools/FileFormat/FileFormatUtil.h>
#include <Tools/FileFormat/FileFormatInfo.h>
#include <Tools/FileFormat/FileFormatType.h>
#include <system/io/file.h>
#include <system/io/directory.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Tools;
using namespace Aspose::Email::Storage::Nsf;
Detect the NSF Format
Before opening a file of unknown origin, ask FileFormatUtil::DetectFileFormat what it is. The returned FileFormatInfo reports FileFormatType::Nsf for a Notes database.
SharedPtr<FileFormatInfo> info = FileFormatUtil::DetectFileFormat(u"SampleNSF.nsf");
if (info->get_FileFormatType() == FileFormatType::Nsf)
{
Console::WriteLine(u"The file is an IBM Notes storage (NSF).");
}
Read the Messages
Open the database with a NotesStorageFacility constructor - a file path or a stream, optionally with NsfLoadOptions - and iterate over EnumerateMessages. Each item is delivered as a MailMessage, so subject, addresses, body and attachments are available exactly as for an EML file.
SharedPtr<NotesStorageFacility> nsf = MakeObject<NotesStorageFacility>(u"SampleNSF.nsf");
// messages come out one by one as regular MailMessage objects
for (SharedPtr<MailMessage> eml : nsf->EnumerateMessages())
{
Console::WriteLine(eml->get_Subject());
}
nsf->Dispose();
Export the Messages from a Stream
The stream constructor is useful when the database is not on the local disk. The example reads the messages and stores each one as an EML file; any other SaveOptions - MSG, MHTML, HTML - would work the same way.
SharedPtr<IO::FileStream> stream = IO::File::OpenRead(u"SampleNSF.nsf");
SharedPtr<NotesStorageFacility> nsf = MakeObject<NotesStorageFacility>(stream);
IO::Directory::CreateDirectory_(u"NsfExport");
int32_t index = 0;
for (SharedPtr<MailMessage> eml : nsf->EnumerateMessages())
{
// any MailMessage output format is available: EML, MSG, MHTML...
eml->Save(String::Format(u"NsfExport\\message_{0}.eml", index++), SaveOptions::get_DefaultEml());
}
Console::WriteLine(u"Exported messages: " + Convert::ToString(index));
nsf->Dispose();
stream->Close();