نوشتن فایل‌های 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 به‌طور مکرر بر روی همان نویسنده فراخوانی کنید؛ هر فراخوانی یک پیام را اضافه می‌کند و یک شناسه رشته‌ای برای آن برمی‌گرداند.