การเขียนไฟล์ 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 ซ้ำ ๆ บนตัวเขียนเดียวกัน; ทุกการเรียกต่อท้ายข้อความหนึ่งข้อความและคืนค่าตัวระบุสตริงสำหรับมัน