MBOX ファイルの書き込み
Contents
[
Hide
]
スクラッチから MBOX アーカイブを作成するか、Thunderbird が既に使用しているものにメッセージを追加するには、 MboxrdStorageWriter クラスです。ライターは通常の MailMessage オブジェクトなので、Aspose.Email で構築またはロードできるもの—EML ファイル、MIME に変換された MSG、コードで組み立てたメッセージ—すべてをストレージに格納できます。
以下のサンプルは次のヘッダーと名前空間を使用しています:
#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 同じライターで繰り返し呼び出すことで、各呼び出しが 1 件のメッセージを追加し、その識別子文字列を返します。