Viết Tệp MBOX
Contents
[
Hide
]
Ghi Tệp MBOX
Nếu bạn cần tạo hoặc cập nhật tệp MBOX một cách lập trình, hãy sử dụng Aspose.Email MboxrdStorageWriter lớp cho phép bạn ghi các tin nhắn mới vào tệp lưu trữ thư của Thunderbird.
Đoạn mã sau cho bạn thấy cách ghi tin nhắn vào lưu trữ thư của Thunderbird:
- Mở tệp lưu trữ Thunderbird trong FileStream.
- Tạo một đối tượng của MboxrdStorageWriter lớp và truyền luồng trên vào hàm khởi tạo.
- Chuẩn bị một tin nhắn mới bằng cách sử dụng MailMessage lớp.
- Gọi WriteMessage() phương thức và truyền những giá trị ở trên MailMessage đối tượng để thêm tin nhắn vào bộ lưu trữ Thunderbird.
- Đóng tất cả các luồng.
// 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();