Quickstart

快速入门:在 C++ 中创建您的第一个 MSG 文件

本指南创建了一个完整、可运行的示例——编写电子邮件、将其保存为 MSG 文件、再次加载并转换为 EML——仅使用标准 C++ 库和 Aspose.Email FOSS for C++。


先决条件

需求细节
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 文件

加载刚创建的文件并打印其属性:

#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

使用 save_to_eml() 将 MSG 文件转换为 RFC 5322 / MIME 格式:

#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 — 许可、格式支持、已知限制

另请参阅

 中文