Quickstart

Quickstart: แฟลล์ MSG ครั้งแรกของคุณใน C++

คู่มือนี้สร้างตัวอย่างที่สามารถทํางานได้เต็มๆ การประกอบอีเมล, จัดเก็บเป็นไฟล์ MSG, โหลดมันกลับ และแปลงมันเป็น EML ?? โดยใช้ห้องสมุด C++ มาตรฐานและ Aspose.Email FOSS สําหรับ C++.


Prerequisites

RequirementDetail
CompilerC++17 หรือหลังกว่า (GCC 9+, Clang 10+, MSVC 2019+)
ระบบการสร้างCMake 3.26 หรือหลังกว่านี้
Libraryคลอนจาก GitHub (ดู) Installation)
DependenciesNone

ขั้นตอน 1: วาง CMakeLists.txt ของคุณ

cmake_minimum_required(VERSION 3.26)
project(email_quickstart)

add_subdirectory(Aspose.Email-FOSS-for-Cpp)

add_executable(quickstart main.cpp)
target_link_libraries(quickstart PRIVATE AsposeEmailFoss::AsposeEmailFoss)
set_target_properties(quickstart PROPERTIES CXX_STANDARD 17)

ขั้นตอนที่ 2: สร้างและบันทึกไฟล์ MSG

สร้างไฟล์ MSG ใหม่ที่มีหัวข้อ, ร่างร่างกาย, ผู้ส่ง, คนรับ และเอกสารแนบ:

#include <fstream>
#include <vector>
#include "aspose/email/foss/msg/mapi_message.hpp"

int main()
{
    // Compose the message
    auto message = aspose::email::foss::msg::mapi_message::create(
        "Hello from Aspose.Email FOSS",
        "This message was created programmatically — no Outlook required.");

    message.set_sender_name("Alice");
    message.set_sender_email_address("alice@example.com");
    message.add_recipient("bob@example.com", "Bob");
    message.add_attachment(
        "readme.txt",
        std::vector<std::uint8_t>{'H', 'e', 'l', 'l', 'o', '\n'},
        "text/plain");

    // Save to MSG file
    std::ofstream out("hello.msg", std::ios::binary);
    message.save(out);

    return 0;
}

สร้างและดําเนินการ:

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/quickstart

รายการนี้ hello.msg เปิดใน Outlook เพื่อตรวจสอบ, หรือดําเนินไปยังขั้นตอนที่ 3 เพื่ออ่านมันกลับตามโปรแกรม.


ขั้นตอนที่ 3: อ่านไฟล์ MSG กลับ

อัตราการอัดไฟล์ที่คุณเพิ่งสร้าง และพิมพ์ Properties ของมัน:

#include <fstream>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"

int main()
{
    std::ifstream input("hello.msg", std::ios::binary);
    const auto message = aspose::email::foss::msg::mapi_message::from_stream(input);

    std::cout << "Subject:     " << message.subject() << '\n';
    std::cout << "From:        " << message.sender_name()
              << " <" << message.sender_email_address() << ">\n";
    std::cout << "Body:        " << message.body() << '\n';
    std::cout << "Recipients:  " << message.recipients().size() << '\n';
    std::cout << "Attachments: " << message.attachments().size() << '\n';

    for (const auto& a : message.attachments())
        std::cout << "  - " << a.filename << " (" << a.data.size() << " bytes)\n";

    return 0;
}

ผลลัพธ์ที่คาดหวัง:

Subject:     Hello from Aspose.Email FOSS
From:        Alice <alice@example.com>
Body:        This message was created programmatically  no Outlook required.
Recipients:  1
Attachments: 1
  - readme.txt (6 bytes)

ขั้นตอนที่ 4: เปลี่ยนเป็น EML

แปลงไฟล์ MSG เป็นรูปแบบ RFC 5322 / MIME ด้วย save_to_eml():

#include <filesystem>
#include <iostream>
#include "aspose/email/foss/msg/mapi_message.hpp"

int main()
{
    const auto message = aspose::email::foss::msg::mapi_message::from_file(
        std::filesystem::path("hello.msg"));

    message.save_to_eml(std::filesystem::path("hello.eml"));
    std::cout << "Saved hello.eml\n";

    return 0;
}

สาระ, ร่างกาย, ผู้ส่ง, คนรับ และเอกสารแนบทั้งหมดถูกรักษาไว้ในผลิต EML.


ขั้นตอนต่อไป

  • Installation การจัดตั้งโครงการ CMake และแพลตฟอร์มที่สนับสนุนทั้งหมด
  • Features การอ้างอิงคุณสมบัติที่ครบถ้วนพร้อมตัวอย่าง C++
  • คู่มือผู้พัฒนา การเดินผ่านทรัพย์สิน MSG, EML, CFB และ MAPI
  • FAQ การอนุญาต, รูปแบบการสนับสนุน, ข้อบกั้นที่ทราบ

See Also

 ภาษาไทย