Quickstart

Quickstart: Tệp MSG đầu tiên của bạn trong C++

Hướng dẫn này tạo ra một ví dụ hoàn chỉnh, có thể chạy - viết một email, lưu nó như một tệp MSG, tải lại và chuyển đổi nó sang EML - chỉ sử dụng thư viện C++ tiêu chuẩn và Aspose.Email FOSS cho C ++ .


Prerequisites

RequirementDetail
CompilerC++17 hoặc hơn (GCC 9+, Clang 10+, MSVC 2019+)
Xây dựng hệ thốngCMake 3.26 hoặc hơn
LibraryCác bài viết từ GitHub (xem) Installation)
DependenciesNone

Bước 1: Tạo CMakeLists.txt của bạn

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)

Bước 2: Tạo và lưu một tệp MSG

Tạo một tệp MSG mới với chủ đề, cơ thể, người gửi, khách hàng và các phần phụ:

#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;
}

Xây dựng và chạy:

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

file của hello.msg được tạo trong thư mục làm việc. mở nó trong Outlook để xác minh, hoặc tiếp tục bước 3 để đọc lại nó một cách lập trình.


Bước 3: Đọc lại tệp MSG

Tải xuống tệp bạn vừa tạo và in các thuộc tính của nó:

#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;
}

Kết quả mong đợi:

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)

Bước 4: Chuyển sang EML

Chuyển đổi tệp MSG sang định dạng RFC 5322 / MIME với 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;
}

Chủ đề, cơ thể, người gửi, nhận và các phụ kiện đều được bảo tồn trong sản xuất EML.


Bước tiếp theo

  • Installation - Cài đặt dự án CMake đầy đủ và các nền tảng được hỗ trợ
  • Features - Kết quả tham khảo đầy đủ với các ví dụ C++
  • Hướng dẫn Developer - MSG, EML, CFB và MAPI Property Walkthroughs
  • FAQ - Giấy phép, hỗ trợ định dạng, giới hạn được biết đến

See Also

 Tiếng Việt