כתיבת קבצי MBOX

יצירת ארכיון MBOX מאפס, או הוספת הודעות למשהו שכבר משתמש Thunderbird, מתבצע עם ה- MboxrdStorageWriter מחלקה. הכותב מקבל רגיל MailMessage עצמים, כך שכל דבר שתוכל לבנות או לטעון עם Aspose.Email - קובץ EML, MSG שהומר ל‑MIME, הודעה שנבנתה בקוד - ניתן למקם באחסון.

הדוגמה למטה משתמשת בכותרות ובמרחבי שמות הבאים:

#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;

הוסף הודעות לקובץ אחסון

הצעדים הם:

  1. פתח את הקובץ יעד בגישה קריאה/כתיבה. FileMode::OpenOrCreate מאפשר לאותו קוד להתחיל ארכיון חדש או להרחיב קיים.
  2. בנה MboxrdStorageWriter על פני הזרם, מעביר MboxSaveOptions מופע.
  3. בנה את ההודעה ומסור אותה ל WriteMessage.
  4. השחרר את הכותב וסגור את הזרם כך שהנתונים המתחת יישפכו.
// 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();

ניתן להעביר שם קובץ במקום זרם - MakeObject<MboxrdStorageWriter>(u"NewMailbox.mbox", options) - במצב זה הכותב פותח וסוגר את הקובץ בעצמו. כדי להוסיף כמה הודעות, קרא WriteMessage חוזר על אותו כותב; כל קריאה מוסיפה הודעה אחת ומחזירה מזהה מחרוזת לה.