บทนำ
Aspose.Email FOSS for C++ คือไลบรารีโอเพ่นซอร์สใหม่สำหรับการอ่าน, สร้าง, และแปลงไฟล์ Outlook MSG, ข้อความ RFC 5322 EML, และคอนเทนเนอร์ Compound File Binary (CFB) ที่เป็นพื้นฐานของรูปแบบ MSG. ไลบรารีเขียนด้วย C++17, ไม่มีการพึ่งพา runtime ภายนอก, และสามารถสร้างบน Linux, macOS, และ Windows ผ่าน CMake — ไม่ต้องพึ่งพา COM และไม่มีเส้นทางโค้ดที่เฉพาะแพลตฟอร์ม.
ไฟล์ Outlook MSG ปรากฏอย่างต่อเนื่องในงานการบูรณาการระดับองค์กร — ท่อการเก็บถาวร, เครื่องมือย้ายข้อมูล, การส่งออกตามข้อกำหนด — แต่การอ่านไฟล์เหล่านี้โดยไม่ใช้ Outlook เองมักต้องพึ่งพาการทำงานอัตโนมัติผ่าน COM บน Windows หรือไลบรารีเชิงพาณิชย์. Aspose.Email FOSS for C++ ให้ทางเลือกที่พกพาได้: คลาส mapi_message ระดับสูงสำหรับการอ่านและสร้างข้อความประจำวัน, พร้อมกับคลาสระดับล่าง msg_reader, msg_document, และ cfb_reader สำหรับการเข้าถึงรูปแบบคอนเทนเนอร์พื้นฐานโดยตรงเมื่อคุณต้องการ.
ไลบรารีนี้ถูกจัดจำหน่ายเป็นซอร์สโค้ด C++17 ภายใต้ใบอนุญาต MIT และเพิ่มเข้ากับโปรเจคเป็นไดเรกทอรีย่อยของ CMake, เปิดเผย target AsposeEmailFoss — ไม่จำเป็นต้องลงทะเบียนกับผู้จัดการแพ็กเกจและไม่ต้องใช้ไบนารีที่สร้างล่วงหน้า.
คุณลักษณะสำคัญ
อ่านไฟล์ Outlook MSG
เปิดไฟล์ .msg ด้วย msg_reader::from_file() หรือ msg_reader::from_stream(). เพื่อเข้าถึง storages และ streams ของข้อความโดยตรง, ห่อหุ้มตัวอ่านด้วย msg_document พร้อมกับ msg_document::from_reader().
#include "aspose/email/foss/msg/msg_reader.hpp"
#include "aspose/email/foss/msg/msg_document.hpp"
#include <filesystem>
namespace aef = aspose::email::foss;
auto reader = aef::msg::msg_reader::from_file(
std::filesystem::path("message.msg"), /*strict=*/false);
auto document = aef::msg::msg_document::from_reader(reader);
สร้างข้อความโดยโปรแกรม
mapi_message::create() สร้างข้อความใหม่ในหน่วยความจำจากหัวเรื่องและเนื้อหา. กำหนดผู้ส่ง, เพิ่มผู้รับ, และแนบข้อมูลไบนารีหรือข้อความก่อนบันทึกด้วย save().
#include "aspose/email/foss/msg/mapi_message.hpp"
#include <filesystem>
namespace aef = aspose::email::foss;
auto message = aef::msg::mapi_message::create(
"Quarterly status update", "Hello team,\n\nRollout summary attached.");
message.set_sender_name("Build Agent");
message.set_sender_email_address("build.agent@example.com");
message.add_recipient("alice@example.com", "Alice Example");
message.add_recipient("carol@example.com", "Carol Example",
aef::msg::mapi_message::recipient_type_cc);
message.add_attachment("report.txt", std::vector<std::uint8_t>{'o', 'k'}, "text/plain");
message.save(std::filesystem::path("output.msg"));
แปลงระหว่าง MSG และ EML
mapi_message::save_to_eml() เขียนข้อความปัจจุบันเป็นไฟล์ EML ตามมาตรฐาน RFC 5322, และ mapi_message::load_from_eml() อ่านไฟล์กลับมา. ทั้งสองทิศทางคงหัวเรื่อง, ผู้ส่ง, ผู้รับ, และไฟล์แนบไว้, ดังนั้นข้อความจึงสามารถย้ายระหว่าง MSG และ EML ได้โดยไม่ต้องใช้ Outlook.
auto message = aef::msg::mapi_message::from_file(
std::filesystem::path("message.msg"), /*strict=*/false);
message.save_to_eml(std::filesystem::path("message.eml"));
auto restored = aef::msg::mapi_message::load_from_eml(std::filesystem::path("message.eml"));
restored.save(std::filesystem::path("restored.msg"));
ตรวจสอบคุณสมบัติของข้อความและไฟล์แนบ
เมื่อโหลด mapi_message แล้ว จะเปิดเผยฟิลด์หัวเรื่องมาตรฐานโดยตรง — subject(), sender_name(), sender_email_address(), message_class(), และ internet_message_id() — พร้อมกับ recipients() และ attachments() ซึ่งเป็นเวกเตอร์ของ mapi_recipient และ mapi_attachment. สำหรับฟิลด์ MAPI ที่ไม่มีตัวเข้าถึงเฉพาะ, get_property_value() จะอ่านคุณสมบัติใด ๆ ด้วย common_message_property_id และ property_type_code.
#include "aspose/email/foss/msg/mapi_message.hpp"
#include "aspose/email/foss/msg/common_message_property_id.hpp"
#include "aspose/email/foss/msg/property_type_code.hpp"
#include <iostream>
namespace aef = aspose::email::foss;
template <typename E>
constexpr auto to_underlying(E value) noexcept {
return static_cast<std::underlying_type_t<E>>(value);
}
auto message = aef::msg::mapi_message::from_file(
std::filesystem::path("message.msg"), /*strict=*/false);
std::cout << "Subject: " << message.subject() << '\n';
std::cout << "From: " << message.sender_name()
<< " <" << message.sender_email_address() << ">\n";
std::cout << "Recipients: " << message.recipients().size() << '\n';
for (const auto& attachment : message.attachments()) {
std::cout << "- " << attachment.filename
<< " (" << attachment.mime_type << ", "
<< attachment.data.size() << " bytes)\n";
}
const auto display_cc = message.get_property_value(
to_underlying(aef::msg::common_message_property_id::display_cc),
to_underlying(aef::msg::property_type_code::ptyp_string));
การเข้าถึงคอนเทนเนอร์ CFB ระดับต่ำ
ไฟล์ MSG ถูกเก็บไว้ภายในคอนเทนเนอร์ Compound File Binary (CFB) — รูปแบบ structured-storage ที่ใช้โดยฟอร์แมตไบนารีของ Office รุ่นเก่า. cfb_reader เปิดคอนเทนเนอร์ดิบและเปิดเผยโครงสร้างไดเรกทรีโดยตรง: storage_ids() และ stream_ids() แสดงรายการทุกรายการ, directory_entry_count() รายงานจำนวนทั้งหมด, และ header() คืนค่าเลย์เอาต์เซกเตอร์ของคอนเทนเนอร์. สิ่งนี้มีประโยชน์เมื่อคุณต้องการข้อมูล MAPI ที่ mapi_message API ไม่แสดงออก.
#include "aspose/email/foss/cfb/cfb_reader.hpp"
#include <iostream>
namespace aef = aspose::email::foss;
auto cfb = aef::cfb::cfb_reader::from_file(std::filesystem::path("message.msg"));
std::cout << "Directory entries: " << cfb.directory_entry_count() << '\n';
std::cout << "Storages: " << cfb.storage_ids().size() << '\n';
std::cout << "Streams: " << cfb.stream_ids().size() << '\n';
เริ่มต้นอย่างเร็ว
เพิ่มไลบรารีลงในโครงการของคุณ:
git clone https://github.com/aspose-email-foss/Aspose.Email-FOSS-for-Cpp.git# CMakeLists.txt
add_subdirectory(Aspose.Email-FOSS-for-Cpp)
target_link_libraries(your_target PRIVATE AsposeEmailFoss::AsposeEmailFoss)จากนั้นสร้างข้อความ บันทึกเป็น MSG แล้วแปลงเป็น EML:
#include "aspose/email/foss/msg/mapi_message.hpp"
#include <filesystem>
#include <vector>
#include <cstdint>
namespace aef = aspose::email::foss;
int main() {
auto message = aef::msg::mapi_message::create("Hello from C++", "Body text here");
message.set_sender_name("Alice");
message.set_sender_email_address("alice@example.com");
message.add_recipient("bob@example.com", "Bob");
message.add_attachment("note.txt", std::vector<std::uint8_t>{'a', 'b', 'c'}, "text/plain");
message.save(std::filesystem::path("hello.msg"));
auto loaded = aef::msg::mapi_message::from_file(
std::filesystem::path("hello.msg"), /*strict=*/false);
loaded.save_to_eml(std::filesystem::path("hello.eml"));
return 0;
}
รูปแบบที่รองรับ
| รูปแบบ | ส่วนขยาย | อ่าน | เขียน |
|---|---|---|---|
| ข้อความ Outlook (MSG) | .msg | ✓ | ✓ |
| EML (RFC 5322 / MIME) | .eml | ✓ | ✓ |
| ไฟล์ไบนารีแบบคอมพาวด์ (CFB) | .cfb | ✓ | ✓ |
ซอร์สเปิดและการให้สิทธิ์
Aspose.Email FOSS สำหรับ C++ ได้รับการปล่อยภายใต้สัญญาอนุญาต MIT. โค้ดเต็มพร้อมให้ดาวน์โหลดที่ aspose-email-foss/Aspose.Email-FOSS-for-Cpp, และการใช้งานเชิงพาณิชย์ได้รับอนุญาตโดยไม่มีค่าลิขสิทธิ์. ไลบรารีไม่มีการพึ่งพา runtime นอกเหนือจากคอมไพเลอร์ C++17 และ CMake 3.26 หรือใหม่กว่า.