การเขียนไฟล์ MBOX

เขียนไฟล์ MBOX

หากคุณต้องการสร้างหรืออัปเดตไฟล์ MBOX programmatically ให้ใช้ Aspose.Email MboxrdStorageWriter คลาสที่ช่วยให้คุณเขียนข้อความใหม่ไปยังไฟล์ที่เก็บเมลของ Thunderbird.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเขียนข้อความไปยังที่เก็บเมลของ Thunderbird:

  1. เปิดไฟล์ที่เก็บของ Thunderbird ใน FileStream.
  2. สร้างอินสแตนซ์ของ MboxrdStorageWriter คลาสและส่งสตรีมที่กล่าวถึงข้างต้นไปยังคอนสตรัคเตอร์.
  3. เตรียมข้อความใหม่โดยใช้ MailMessage คลาส.
  4. เรียกใช้ WriteMessage() เมธอดและส่งค่าที่กล่าวถึงข้างต้น MailMessage อินสแตนซ์เพื่อเพิ่มข้อความไปยังที่เก็บของ Thunderbird.
  5. ปิดสตรีมทั้งหมด.
// Open the storage file with FileStream
var stream = new FileStream(dataDir + "ExampleMbox.mbox", FileMode.Open, FileAccess.Write);

// Initialize MboxStorageWriter and pass the above stream to it
var writer = new MboxrdStorageWriter(stream, false);
// Prepare a new message using the MailMessage class
var message = new MailMessage("from@domain.com", "to@domain.com", Guid.NewGuid().ToString(), "added from Aspose.Email");
message.IsDraft = false;
// Add this message to storage
writer.WriteMessage(message);
// Close all related streams
writer.Dispose();
stream.Close();