Tạo và Lưu tệp Outlook MSG

Aspose.Email for C++ cho phép các nhà phát triển lập trình tạo, sửa đổi và lưu các tệp Outlook MSG với toàn quyền kiểm soát các thuộc tính và định dạng tin nhắn. Bạn có thể tạo tin MSG từ đầu, thêm tệp đính kèm, sử dụng nội dung văn bản đa dạng (RTF), lưu bản nháp và tối ưu kích thước tin nhắn bằng các tùy chọn nén nội dung.

Các mẫu trong bài viết này sử dụng các tiêu đề và không gian tên sau:

#include <MailMessage.h>
#include <MsgLoadOptions.h>
#include <Attachment.h>
#include <AttachmentCollection.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiMessageFlags.h>
#include <Mapi/MapiConversionOptions.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <system/array.h>
#include <system/convert.h>
#include <system/io/file.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;

Tạo và Lưu Tin nhắn Outlook

Một tin nhắn thường được soạn thảo dưới dạng một MailMessage: đặt người gửi, người nhận, tiêu đề và phần thân, sau đó chuyển nó thành một mục Outlook bằng MapiMessage::FromMailMessage. MapiMessage::Save ghi mục như một tệp MSG mà Outlook mở trực tiếp.

SharedPtr<MailMessage> mailMsg = MakeObject<MailMessage>();
mailMsg->set_From(u"sender@domain.com");
mailMsg->set_To(u"receiver@domain.com");
mailMsg->set_Subject(u"This is a test message");
mailMsg->set_Body(u"This is a test body");

SharedPtr<MapiMessage> outlookMsg = MapiMessage::FromMailMessage(mailMsg);
outlookMsg->Save(u"CreatedMessage.msg");

Console::WriteLine(u"Saved: " + MapiMessage::Load(u"CreatedMessage.msg")->get_Subject());

Thêm Người Nhận và Đính Kèm vào MapiMessage

Một MapiMessage cũng có thể được điền trực tiếp, mà không cần chuyển đổi một MailMessage. Thuộc tính Thêm phương thức của MapiRecipientCollection lớp trả về đối tượng được tạo MapiRecipient, và Thêm phương thức của MapiAttachmentCollection lớp trả về đối tượng được tạo MapiAttachment. Điều này cho phép bạn cấu hình ngay một người nhận hoặc tệp đính kèm mới, mà không cần tra cứu riêng trong bộ sưu tập.

Loại người nhận là một trong các MapiRecipientType các giá trị. Đoạn mã sau thêm một người nhận và một tệp đính kèm, sau đó thay đổi tên hiển thị của chúng:

#include <Mapi/MapiMessage.h>
#include <Mapi/MapiRecipient.h>
#include <Mapi/MapiRecipientCollection.h>
#include <Mapi/MapiRecipientType.h>
#include <Mapi/MapiAttachment.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <system/io/file.h>

using namespace System;
using namespace Aspose::Email::Mapi;

auto message = MakeObject<MapiMessage>();
message->set_Subject(u"Invoice");

// Add a recipient and configure it
SharedPtr<MapiRecipient> recipient = message->get_Recipients()->Add(u"alice.johnson@example.com", u"SMTP", u"Alice Johnson", MapiRecipientType::MAPI_TO);
recipient->set_DisplayName(u"Alice Johnson (Accounting)");

// Add an attachment from file bytes and configure it
ArrayPtr<uint8_t> fileBytes = IO::File::ReadAllBytes(u"invoice.pdf");
SharedPtr<MapiAttachment> attachment = message->get_Attachments()->Add(u"invoice.pdf", fileBytes);
attachment->set_DisplayName(u"Invoice #2026-001.pdf");

message->Save(u"invoice.msg");

Tạo Tệp MSG với Nội dung RTF

Outlook giữ phần thân được định dạng của một tin nhắn dưới dạng Rich Text Format (RTF). Khi một MailMessage với phần thân HTML được chuyển đổi, Aspose.Email xây dựng phần RTF từ HTML, vì vậy tiêu đề, chữ đậm và chữ gạch dưới vẫn tồn tại trong tệp MSG. Văn bản RTF có sẵn qua get_BodyRtf.

SharedPtr<MailMessage> mailMsg = MakeObject<MailMessage>();
mailMsg->set_From(u"from@domain.com");
mailMsg->set_To(u"to@domain.com");
mailMsg->set_Subject(u"RTF example");
mailMsg->set_HtmlBody(u"<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>");

SharedPtr<MapiMessage> outlookMsg = MapiMessage::FromMailMessage(mailMsg);
outlookMsg->Save(u"MessageWithRtfBody.msg");

String rtf = MapiMessage::Load(u"MessageWithRtfBody.msg")->get_BodyRtf();
Console::WriteLine(u"RTF body starts with: " + rtf.Substring(0, 6));

Lưu tin nhắn ở trạng thái bản nháp

Outlook hiển thị tin nhắn dưới dạng bản nháp - có thể chỉnh sửa và có nút Send - khi MSGFLAG_UNSENT cờ được đặt trên nó. Mẫu tải một tệp MSG hiện có như một MailMessage (với MsgLoadOptions), thay đổi nó và lưu lại dưới dạng bản nháp bằng cách truyền cờ tới SetMessageFlags.

SharedPtr<MailMessage> msg = MailMessage::Load(u"Message.msg", MakeObject<MsgLoadOptions>());
msg->set_Subject(msg->get_Subject() + u" (updated by Aspose.Email)");
msg->set_HtmlBody(u"<p>NEW BODY (updated by Aspose.Email)</p>");

SharedPtr<MapiMessage> mapiMsg = MapiMessage::FromMailMessage(msg);
mapiMsg->SetMessageFlags(MapiMessageFlags::MSGFLAG_UNSENT);
mapiMsg->Save(u"DraftMessage.msg");

bool isDraft = (MapiMessage::Load(u"DraftMessage.msg")->get_Flags() & MapiMessageFlags::MSGFLAG_UNSENT) == MapiMessageFlags::MSGFLAG_UNSENT;
Console::WriteLine(u"Saved as a draft: " + Convert::ToString(isDraft));

Tối ưu hoá với Nén Nội dung

Nội dung của tệp MSG có thể được lưu trữ dưới dạng nén. MapiConversionOptions::set_UseBodyCompression bật nén cho quá trình chuyển đổi; các tệp kết quả - và các tệp PST được tạo từ các tin nhắn như vậy - sẽ nhỏ hơn, với chi phí là một chút thời gian xử lý bổ sung.

SharedPtr<MailMessage> message = MailMessage::Load(u"Message.eml");

SharedPtr<MapiConversionOptions> options = MakeObject<MapiConversionOptions>();
options->set_UseBodyCompression(true);
MapiMessage::FromMailMessage(message, options)->Save(u"CompressedBody.msg");

MapiMessage::FromMailMessage(message)->Save(u"UncompressedBody.msg");

Console::WriteLine(u"Compressed body:   " + Convert::ToString(IO::File::ReadAllBytes(u"CompressedBody.msg")->get_Length()) + u" bytes");
Console::WriteLine(u"Uncompressed body: " + Convert::ToString(IO::File::ReadAllBytes(u"UncompressedBody.msg")->get_Length()) + u" bytes");
  • UseBodyCompression = true → kích thước tệp nhỏ hơn, hiệu năng chậm hơn.
  • UseBodyCompression = false → chuyển đổi nhanh hơn, kích thước lớn hơn.

Tạo Tệp MSG với Các Tệp Đính Kèm

Tệp đính kèm được thêm vào MailMessage trước khi được chuyển đổi: mọi Attachment trong Tệp Đính Kèm bộ sưu tập trở thành tệp đính kèm của tin nhắn Outlook kết quả. Xây dựng mục trực tiếp như một MapiMessage được mô tả trong Quản lý Tệp Đính Kèm trong Tệp Outlook MSG.

SharedPtr<MailMessage> mailMsg = MakeObject<MailMessage>();
mailMsg->set_From(u"sender@domain.com");
mailMsg->set_To(u"receiver@domain.com");
mailMsg->set_Subject(u"This is a test message");
mailMsg->set_Body(u"This is a test body");

// every file added here becomes an attachment of the MSG
mailMsg->get_Attachments()->Add(MakeObject<Attachment>(u"1.jpg"));
mailMsg->get_Attachments()->Add(MakeObject<Attachment>(u"description.html"));

SharedPtr<MapiMessage> outlookMsg = MapiMessage::FromMailMessage(mailMsg);
outlookMsg->Save(u"MessageWithAttachments.msg");

Console::WriteLine(u"Attachments in the saved message: " +
    Convert::ToString(MapiMessage::Load(u"MessageWithAttachments.msg")->get_Attachments()->get_Count()));