EML và MSG chuyển đổi

EML và MSG chuyển đổi

Aspose.Email FOSS for C++ converts bidirectionally between EML (RFC 5322 / MIME) và MSG (Outlook MAPI) định dạng sử dụng mapi_message.Con đường chuyển đổi là: load_from_eml() trong bộ nhớ mapi_message → save() cho EML→MSG, và from_file() / from_stream() → save_to_eml() Đối với MSG→EML.


Chuyển đổi EML sang MSG

tải một tập tin EML với mapi_message::load_from_eml(),Sau đó, hãy tham gia MSG với save():

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

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

    std::cout << "Subject: " << message.subject() << '\n';
    message.save(std::filesystem::path("converted.msg"));
    std::cout << "Saved converted.msg\n";
}

load_from_eml() phân đoạn cấu trúc MIME và dân số hóa chủ đề MAPI, văn bản thẳng cơ thể, cơ quan HTML, danh tính gửi, Danh sách người nhận và tất cả các phần liên kết.


Chuyển đổi MSG sang EML

tải một tệp MSG với mapi_message::from_file(),Sau đó gọ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("sample.msg"));

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

Chuyển đổi dựa trên Stream

4 phương pháp thích hợp chấp nhận std::istream / std::ostream tải quá cho - xử lý theo kiểu ống mà không chạm vào hệ thống tệp:

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

int main()
{
    // EML → MSG via streams
    std::ifstream eml_in("message.eml", std::ios::binary);
    const auto message = aspose::email::foss::msg::mapi_message::load_from_eml(eml_in);

    std::ofstream msg_out("converted.msg", std::ios::binary);
    message.save(msg_out);

    // MSG → EML via streams
    std::ifstream msg_in("converted.msg", std::ios::binary);
    const auto reloaded = aspose::email::foss::msg::mapi_message::from_stream(msg_in);

    std::ofstream eml_out("roundtrip.eml", std::ios::binary);
    reloaded.save_to_eml(eml_out);
}

Luôn mở dòng với std::ios::binary. Text-mode stream dịch kết thúc dòng trên Windows, phá hủy dữ liệu CFB nhị phân.


In-Memory Conversion (Tạm dịch: Chuyển đổi bộ nhớ)

save() và save_to_eml() Cả hai đều có 0-argument overload trở lại std::vector<std::uint8_t>.Sử dụng các điều này để tránh tất cả hệ thống tệp I/O:

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

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

    const std::vector<std::uint8_t> msg_bytes  = message.save();
    const std::vector<std::uint8_t> eml_bytes  = message.save_to_eml();

    // Write to a database, network socket, or other destination...
}

Thay đổi Batch Directory

Tạo một thư mục với std::filesystem::directory_iterator Chuyển đổi tất cả .eml Các file trong 1 pass:

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

int main()
{
    const std::filesystem::path input_dir("emails/eml");
    const std::filesystem::path output_dir("emails/msg");
    std::filesystem::create_directories(output_dir);

    for (const auto& entry : std::filesystem::directory_iterator(input_dir))
    {
        if (entry.path().extension() != ".eml")
            continue;

        const auto message = aspose::email::foss::msg::mapi_message::load_from_eml(
            entry.path());

        auto out_path = output_dir / entry.path().filename();
        out_path.replace_extension(".msg");
        message.save(out_path);

        std::cout << "Converted: " << entry.path().filename() << '\n';
    }
}

Mẹo và Thực hành Tốt nhất

  • Mở tất cả các file stream với std::ios::binary - Cả nhập và xuất khẩu.
  • Prefer load_from_eml(path) qua dòng chảy quá tải khi bạn có một con đường; con Đường quá tải quản lý mở file trong và đơn giản hơn một chút.
  • Sử dụng các byte quá tải (save() và save_to_eml() Không có thông tin (từ khi thu thập dữ liệu) giữa các dịch vụ để tránh tệp tạm thời.
  • Chủ đề, cơ thể, HTML cơ quan, người gửi, nhận và các phần bổ sung MIME đều được bảo tồn thông qua toàn bộ vòng tròn.
  • Các lá cờ IMAP, đầu hướng và nội dung mã hóa TNEF không được thực hiện thông qua chuyển đổi.

Các vấn đề thường gặp

IssueCauseFix Đặt
load_from_eml ném vào một tệp có hiệu lựcKết thúc đường không CRLF trong EMLBảo đảm EML sử dụng RFC 5322 CRLF; một số công cụ xuất khẩu viết LF đơn giản
Các phụ kiện bị mất sau EML → MSGMIME không chuẩn mã hóaKiểm tra EML sử dụng multipart/mixed với tiêu chuẩn Content-Disposition: attachment
Cơ thể HTML trống sau MSG → EMLNguồn MSG chỉ có cơ thể văn bản thẳngCheck message.html_body();· nếu trống, chỉ có văn bản thẳng được lưu trữ
Xuất khẩu dòng chảy bị hư hạiStream mở trong chế độ văn bảnLuôn sử dụng std::ios::binary
Subject hiển thị ?= Các sequenceRFC 2047 mã hóa-được viết bằng EML ban đầuXóa bằng thư viện RFC 2047; thư ký giữ hình thức mã hóa

FAQ

EML → MSG có bảo vệ cơ thể HTML không?

Có A text/html MIME phần được lưu trữ trong tài sản cơ thể HTML của MAPI và có sẵn qua message.html_body() sau khi tải.

Các phụ kiện được bảo tồn trong cả hai hướng?

Tất cả các bộ phận MIME được vận chuyển thông qua mapi_message Danh sách attachment. Dữ liệu nhị phân được cung cấp như: attachment.data (a) std::vector<std::uint8_t>).

Tôi có thể chuyển đổi một EML sang nhiều định dạng xuất xứ cùng lúc không?

Đặt EML một lần với load_from_eml(),Sau đó gọi cả hai save() và save_to_eml() Trong cùng một bộ nhớ mapi_message đối tượng.

là load_from_eml() Thread an toàn?

Mỗi cuộc gọi tạo ra một độc lập mapi_message Ví dụ: Nhiều dây có thể gọi load_from_eml() đồng thời miễn là mỗi thread sử dụng đối tượng phát hành riêng của nó.

Điều gì xảy ra với tin nhắn không có cơ thể văn bản thẳng?

Nếu EML chỉ chứa một text/html Phần , message.body() Lại lấy một dòng trống và message.html_body() mang nội dung HTML. cả hai đều được bảo quản thông qua MSG Phân tích.


Tóm tắt Tham chiếu API

Class / Phương phápDescription
mapi_message::load_from_eml(path)Chia một tập tin EML vào A mapi_message
mapi_message::load_from_eml(stream)Eml từ một std::istream
mapi_message::save(path)Serialize mapi_message - Tải file MSG
mapi_message::save(stream)Viết các byte MSG cho một std::ostream
mapi_message::save()Trở lại MSG như std::vector<std::uint8_t>
mapi_message::from_file(path)tải một tệp MSG vào A mapi_message
mapi_message::from_stream(stream)tải MSG từ một std::istream
mapi_message::save_to_eml(path)Viết EML vào một đường file
mapi_message::save_to_eml(stream)Viết EML byte cho một std::ostream
mapi_message::save_to_eml()Trở về EML như std::vector<std::uint8_t>

See Also

 Tiếng Việt