Viết Tệp MBOX

Tạo một kho lưu trữ MBOX từ đầu, hoặc thêm tin nhắn vào một kho mà Thunderbird đã sử dụng, được thực hiện bằng MboxrdStorageWriter lớp. Trình ghi nhận các MailMessage đối tượng, vì vậy bất cứ thứ gì bạn có thể xây dựng hoặc tải với Aspose.Email - một tệp EML, một MSG đã chuyển đổi sang MIME, một tin nhắn được lắp ráp trong mã - có thể được đặt vào kho lưu trữ.

Mẫu dưới đây sử dụng các tiêu đề và không gian tên sau:

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

Thêm Tin Nhắn vào Tệp Lưu Trữ

Các bước là:

  1. Mở tệp mục tiêu với quyền đọc/ghi. FileMode::OpenOrCreate cho phép cùng một mã bắt đầu một kho lưu trữ mới hoặc mở rộng một kho hiện có.
  2. Tạo một MboxrdStorageWriter trên luồng, truyền một MboxSaveOptions thể hiện.
  3. Xây dựng tin nhắn và chuyển nó cho WriteMessage.
  4. Giải phóng trình ghi và đóng luồng để dữ liệu dư cuối cùng được đẩy ra.
// 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();

Tên tệp có thể được truyền thay cho luồng - MakeObject<MboxrdStorageWriter>(u"NewMailbox.mbox", options) - trong trường hợp này trình ghi mở và đóng tệp tự mình. Để thêm nhiều tin nhắn, gọi WriteMessage lặp lại trên cùng một trình ghi; mỗi lần gọi nối một tin nhắn và trả về một định danh chuỗi cho nó.