บทนำ
การทำงานกับไฟล์ Outlook MSG ใน C++ ในอดีตต้องใช้การทำงานอัตโนมัติ COM ที่จำกัดเฉพาะ Windows
หรือไลบรารีของบุคคลที่สามที่มีขนาดใหญ่และมีลิขสิทธิ์จำกัด Aspose.Email FOSS for C++
เปลี่ยนแปลงสิ่งนั้น: เป็นไลบรารี C++17 แท้ ๆ ที่ไม่มีการพึ่งพาภายนอก, ไม่มี COM, และไม่มี
โค้ดที่จำเพาะกับแพลตฟอร์ม มันสร้างได้อย่างเหมือนกันบน Linux, macOS, และ Windows.
ไลบรารีนี้ถูกเผยแพร่ภายใต้สัญญาอนุญาต MIT และแจกจ่ายเป็นซอร์สโค้ดผ่าน GitHub. คุณ ทำการรวมโดยการโคลนรีโพซิทอรีและเพิ่มเป็นไดเรกทอรีย่อยของ CMake — ไม่จำเป็นต้องลงทะเบียนกับผู้จัดการแพ็กเกจ, และไม่ต้องสร้างไบนารีล่วงหน้า.
โพสต์นี้อธิบายขั้นตอนหลัก: การอ่านไฟล์ MSG ที่มีอยู่, การตรวจสอบคุณสมบัติของไฟล์, การสร้างข้อความใหม่โดยโปรแกรม, และการแปลงกลับไปกลับมาระหว่างรูปแบบ MSG และ EML.
คุณสมบัติหลัก
อ่านไฟล์ MSG ที่มีอยู่
ใช้ msg_reader เพื่อเปิดไฟล์ .msg และ msg_document เพื่อเข้าถึงเนื้อหาแบบมีโครงสร้างของมัน
ทั้งสองคลาสอยู่ในเนมสเปซ aspose::email::foss::msg.
#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"));
auto document = aef::msg::msg_document::from_reader(reader);
msg_reader ยังมีการ overload from_stream สำหรับการอ่านจาก std::istream ซึ่งเป็นประโยชน์ในบริบทของ server‑side หรือ pipeline ที่ไฟล์ไม่ได้อยู่บนดิสก์.
สร้างข้อความโดยใช้โปรแกรม
mapi_message::create สร้างข้อความใหม่ในหน่วยความจำพร้อมหัวเรื่องและเนื้อหา คุณ
สามารถตั้งค่ารายละเอียดผู้ส่ง เพิ่มผู้รับ และแนบข้อมูลไบนารีหรือข้อความก่อน
บันทึกได้.
#include "aspose/email/foss/msg/mapi_message.hpp"
namespace aef = aspose::email::foss;
auto msg = aef::msg::mapi_message::create(
"Quarterly status update",
"Hello team,\n\nPlease find the rollout summary attached.\n\nRegards");
msg.set_sender_name("Build Agent");
msg.set_sender_email_address("build.agent@example.com");
msg.add_recipient("alice@example.com", "Alice Example");
msg.save(std::filesystem::path("output.msg"));
แปลง MSG เป็น EML
mapi_message รองรับการแปลงโดยตรงจาก MSG ไปเป็น EML ผ่านวิธี save_to_eml.
ผลลัพธ์ EML ปฏิบัติตาม RFC 5322 (MIME) ทำให้เข้ากันได้กับไคลเอนต์เมลมาตรฐานใด ๆ
หรือสายการประมวลผล.
auto loaded = aef::msg::mapi_message::from_file(
std::filesystem::path("message.msg"), /*strict=*/false);
// Save as EML to a file
loaded.save_to_eml(std::filesystem::path("message.eml"));
// Or capture as bytes
std::vector<std::uint8_t> eml_bytes = loaded.save_to_eml();
โหลดจาก EML
ทิศทางย้อนกลับก็เท่าเดิมที่ง่ายเช่นกัน: load_from_eml อ่านไฟล์ EML และสร้าง mapi_message ในหน่วยความจำ ซึ่งจากนั้นสามารถบันทึกกลับเป็น MSG ได้.
auto from_eml = aef::msg::mapi_message::load_from_eml(
std::filesystem::path("message.eml"));
from_eml.save(std::filesystem::path("restored.msg"));
การเข้าถึง CFB ระดับต่ำ
สำหรับกรณีการใช้งานขั้นสูง — เช่น การอ่านคุณสมบัติ MAPI ดิบที่ไม่ได้เปิดเผยโดย API ระดับสูง — cfb_reader ให้การเข้าถึงโดยตรงไปยังคอนเทนเนอร์ Compound File Binary ที่เป็นพื้นฐานของรูปแบบ MSG.
#include "aspose/email/foss/cfb/cfb_reader.hpp"
namespace aef = aspose::email::foss;
auto cfb = aef::cfb::cfb_reader::from_file(std::filesystem::path("message.msg"));
auto storage_ids = cfb.storage_ids();
auto stream_ids = cfb.stream_ids();
เริ่มต้นอย่างรวดเร็ว
ขั้นตอนที่ 1 — คัดลอกและเพิ่มเป็นไดเรกทอรีย่อย
git clone https://github.com/aspose-email-foss/Aspose.Email-FOSS-for-Cpp.git
ในโครงการของคุณ CMakeLists.txt:
cmake_minimum_required(VERSION 3.26)
project(my_project)
add_subdirectory(Aspose.Email-FOSS-for-Cpp)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE AsposeEmailFoss::AsposeEmailFoss)
ขั้นตอนที่ 2 — รวมและใช้
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"
namespace aef = aspose::email::foss;
int main()
{
// Create a message
auto msg = aef::msg::mapi_message::create("Hello from C++", "Body text here");
msg.save(std::filesystem::path("hello.msg"));
// Round-trip to EML
auto loaded = aef::msg::mapi_message::from_file(
std::filesystem::path("hello.msg"), false);
loaded.save_to_eml(std::filesystem::path("hello.eml"));
std::cout << "Done\n";
}
ขั้นตอนที่ 3 — สร้าง
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
รูปแบบที่รองรับ
| รูปแบบ | ส่วนขยาย | อ่าน | เขียน |
|---|---|---|---|
| MSG (ข้อความ Outlook) | .msg | ✓ | ✓ |
| EML (RFC 5322 / MIME) | .eml | ✓ | ✓ |
| CFB (Compound File Binary) | .msg, .cfb | ✓ | ✓ |
โอเพนซอร์สและการให้สิทธิ์
Aspose.Email FOSS for C++ ถูกปล่อยภายใต้สัญญาอนุญาต MIT. โค้ดต้นฉบับเต็มพร้อมให้ดาวน์โหลดบน GitHub ที่ aspose-email-foss/Aspose.Email-FOSS-for-Cpp. การใช้งานเชิงพาณิชย์ได้รับอนุญาตโดยไม่มีค่าลิขสิทธิ์. ไลบรารีไม่มีการพึ่งพา runtime ใด ๆ นอกจากคอมไพเลอร์ C++17 และ CMake 3.26 หรือใหม่กว่า.