כתיבת קבצי MBOX
Contents
[
Hide
]
יצירת ארכיון 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;
הוסף הודעות לקובץ אחסון
הצעדים הם:
- פתח את הקובץ יעד בגישה קריאה/כתיבה.
FileMode::OpenOrCreateמאפשר לאותו קוד להתחיל ארכיון חדש או להרחיב קיים. - בנה MboxrdStorageWriter על פני הזרם, מעביר MboxSaveOptions מופע.
- בנה את ההודעה ומסור אותה ל WriteMessage.
- השחרר את הכותב וסגור את הזרם כך שהנתונים המתחת יישפכו.
// 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();
פתח את הזרם עם
FileAccess::ReadWrite, לא FileAccess::Write: הכותב צריך לקרוא את תוכן הקובץ הקיים לפני שהוא מוסיף אליו, וזרם כתיבה בלבד גורם לתקלה עם NotSupportedException.
ניתן להעביר שם קובץ במקום זרם - MakeObject<MboxrdStorageWriter>(u"NewMailbox.mbox", options) - במצב זה הכותב פותח וסוגר את הקובץ בעצמו. כדי להוסיף כמה הודעות, קרא WriteMessage חוזר על אותו כותב; כל קריאה מוסיפה הודעה אחת ומחזירה מזהה מחרוזת לה.