Introduction

การทํางานกับไฟล์ Outlook MSG ใน C++ ได้ใช้ระบบ Windows-only COM อัตโนมัติ หรือห้องสมุดของผู้บริการที่มีความจํากัด Aspose. Email FOSS for C ++ เปลี่ยนแปลงว่า: มันเป็นห้องรวมแบบ C+++17 ที่ไม่มีความพึ่งจากภายนอก, ไม่มี COM และไม่มีรหัสเฉพาะแพลตฟอร์ม มันสร้างเหมือนกันบน Linux, macOS และ Windows.

ห้องสมุดถูกปล่อยลงภายใต้ลิขสิทธิ์ MIT และจําหน่ายเป็นแหล่งผ่าน GitHub คุณสามารถรวมมันโดยการคลอนคลังและเพิ่มมันเป็น CMake ไม่จําเป็นต้องลงทะเบียนผู้จัดการแพ็คเกจ และไม่จําเป็นที่จะสร้างไบนารีก่อนหน้า.

บทความนี้เดินผ่านกระแสการทํางานหลัก: การอ่านไฟล์ MSG ที่มีอยู่, ตรวจสอบคุณสมบัติของมัน, สร้างข้อความใหม่โดยโปรแกรม และไป-กลับระหว่างรูปแบบ MS G กับ EML.


คุณสมบัติหลัก

อ่านไฟล์ MSG ที่มีอยู่

การใช้งาน msg_reader เปิดตัว .msg รายการและ msg_document เพื่อเข้าถึงเนื้อหาที่มีโครงสร้างของมัน. ทั้งสองชั้นอาศัยอยู่ใน aspose::email::foss::msg namespace (พื้นที่ชื่อ).

#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 มีการพัฒนาที่ from_stream การใช้งานที่เกินการอ่านจาก std::istream, ซึ่งเป็นประโยชน์ในเว็บไซต์ด้านเซอร์หรือ pipeline ที่ไฟล์ไม่มีอยู่บนดิสก์.

การสร้างข้อความโดยการเขียนโปรแกรม

mapi_message::create สร้างข้อความใหม่ในความจํา ด้วยหัวข้อและร่างกาย คุณ จากนั้นสามารถตั้งข้อมูลผู้ส่ง เติมตัวรับ และติดพายไบนารีหรือข้อความก่อนการใช้งาน การออมเงิน.

#include "aspose/email/foss/msg/mapi_message.hpp"

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 output สอดคล้องกับ 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 ซึ่งเป็นพื้นฐานของรูปแบบ MSG.

#include "aspose/email/foss/cfb/cfb_reader.hpp"

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"

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

รูปแบบที่สนับสนุน

FormatExtensionReadWrite
MSG (ข้อความวิสัยทัศน์).msg
EML (RFC 5322 / MIME).eml
CFB (Compound File Binary).msg, .cfb

หลักสูตรและใบอนุญาต

Aspose.Email FOSS for C++ is released under the MIT license. The full source is available ใน GitHub ที่: aspose-email-foss/Aspose.Email-FOSS-for-Cpp การจัดสรรของผู้ประกอบการ. การใช้งานทางการค้าถูกอนุญาตโดยไม่มีค่าบริจาค ห้องสมุดนี้ไม่มีความพึ่งพาเวลาทํางาน มากกว่าคอมพิลเลอร์ C++17 และ CMake 3.26 หรือหลังกว่านี้.


เริ่มต้น