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, not FileAccess::Write: 작성자는 파일에 추가하기 전에 기존 내용을 읽어야 하며, 쓰기 전용 스트림은 이를 실패하게 합니다. NotSupportedException.
스트림 대신 파일 이름을 전달할 수 있습니다 - MakeObject<MboxrdStorageWriter>(u"NewMailbox.mbox", options) - 이 경우 작성자는 파일을 스스로 열고 닫습니다. 여러 메시지를 추가하려면 호출하세요 WriteMessage 같은 작성자에 대해 반복적으로 호출합니다; 각 호출은 하나의 메시지를 추가하고 해당 메시지의 문자열 식별자를 반환합니다.