Scrittura di File MBOX

Creare un archivio MBOX da zero, o aggiungere messaggi a quello già usato da Thunderbird, è fatto con il MboxrdStorageWriter classe. Il writer accetta input ordinari MailMessage oggetti, quindi qualsiasi cosa tu possa costruire o caricare con Aspose.Email - un file EML, un MSG convertito in MIME, un messaggio assemblato nel codice - può essere inserito nell’archivio.

L’esempio qui sotto usa le seguenti intestazioni e spazi dei nomi:

#include <MailMessage.h>
#include <Storage/Mbox/MboxrdStorageWriter.h>
#include <Storage/Mbox/MboxSaveOptions.h>
#include <system/io/file_stream.h>
#include <system/guid.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Storage::Mbox;

Aggiungi messaggi a un file di archiviazione

I passaggi sono:

  1. Apri il file di destinazione con accesso in lettura/scrittura. FileMode::OpenOrCreate consente allo stesso codice di avviare un nuovo archivio o di estendere quello esistente.
  2. Costruisci un MboxrdStorageWriter sul flusso, passando un MboxSaveOptions istanza.
  3. Costruisci il messaggio e passalo a WriteMessage.
  4. Elimina il writer e chiudi il flusso in modo che i dati finali vengano scritti.
// Open (or create) the storage file for writing
SharedPtr<IO::FileStream> stream = MakeObject<IO::FileStream>(u"NewMailbox.mbox", IO::FileMode::OpenOrCreate, IO::FileAccess::ReadWrite);

// Initialize the writer over the stream
SharedPtr<MboxrdStorageWriter> writer = MakeObject<MboxrdStorageWriter>(stream, MakeObject<MboxSaveOptions>());

// Prepare a message
SharedPtr<MailMessage> message = MakeObject<MailMessage>(u"from@domain.com", u"to@domain.com",
    Guid::NewGuid().ToString(), u"added from Aspose.Email for C++");
message->set_IsDraft(false);

// Append the message to the storage
writer->WriteMessage(message);

// Release the writer and the stream
writer->Dispose();
stream->Close();

È possibile passare un nome di file invece di un flusso - MakeObject<MboxrdStorageWriter>(u"NewMailbox.mbox", options) - nel qual caso il writer apre e chiude il file da solo. Per aggiungere più messaggi, chiama WriteMessage ripetutamente sullo stesso writer; ogni chiamata aggiunge un messaggio e restituisce un identificatore stringa per esso.