วิธีการแปลง EML เป็น MSG ใน C++

วิธีการแปลง EML เป็น MSG ใน C++

Aspose.Email FOSS for C++ supports bidirectional conversion between EML (RFC 5322 / MIME) และ MSG (Outlook MAPI ) รูปแบบ ใช้ mapi_message::load_from_eml() สําหรับการนําเข้า EML ไฟล์แล้ว mapi_message::save() เขียนเป็น MSG - และ vice versa กับ mapi_message::from_file() ถือตามโดย mapi_message::save_to_eml().

ขั้นตอนที่ 1 - สร้างโครงการ

git clone https://github.com/aspose-email-foss/Aspose.Email-FOSS-for-Cpp.git
add_subdirectory(Aspose.Email-FOSS-for-Cpp)
target_link_libraries(your_target PRIVATE AsposeEmailFoss::AsposeEmailFoss)

ขั้นตอนที่ 2 — แปลง EML เป็น MSG (เส้นทางไฟล์)

mapi_message::load_from_eml() แปลงไฟล์ EML และเปิดเผยคุณสมบัติผ่านทาง มาตรฐาน mapi_message อินเตอร์เฟซ โทรศัพท์ save() เพื่อเขียนผลลัพธ์เป็น MSG:

#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';
    std::cout << "From:    " << message.sender_email_address() << '\n';

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

ขั้นตอนที่ 3 — แปลง EML ไปยัง MSG (Stream)

ใช้การโหลดเกินของกระแส load_from_eml() เมื่อข้อมูล EML มาจากเครือข่าย socket, buffer หน่วยความจําหรืออื่น ๆ std::istream:

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

int main()
{
    std::ifstream eml_stream("message.eml", std::ios::binary);
    const auto message = aspose::email::foss::msg::mapi_message::load_from_eml(eml_stream);

    message.save(std::filesystem::path("converted.msg"));
}

ขั้นตอนที่ 4 — แปลง MSG ไปยัง EML

โหลดไฟล์ MSG ที่มีอยู่ด้วย mapi_message::from_file(), แล้ว serialize ไปยัง EML รูปแบบด้วย 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";
}

ขั้นตอนที่ 5 — การเดินทางรอบเต็ม (EML → MSG→ EML)

ตัวอย่างต่อไปนี้แสดงให้เห็นว่าหัวข้อร่างกายผู้ส่งผู้รับและแถบแนบทั้งหมดจะถูกเก็บรักษาผ่านการเดินทางรอบเต็มรูปแบบ:

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

int main()
{
    const auto msg_path = std::filesystem::path("roundtrip.msg");
    const auto eml_out  = std::filesystem::path("roundtrip.eml");

    // EML → MSG
    auto message = aspose::email::foss::msg::mapi_message::load_from_eml(
        std::filesystem::path("original.eml"));
    message.save(msg_path);

    // MSG → EML
    auto reloaded = aspose::email::foss::msg::mapi_message::from_file(msg_path);
    reloaded.save_to_eml(eml_out);

    std::cout << "Subject preserved: " << reloaded.subject() << '\n';
    std::cout << "Attachments: " << reloaded.attachments().size() << '\n';
}

ขั้นตอนที่ 6 — แปลงเป็น EML บิตในหน่วยความจํา

ทั้งสอง save() และ save_to_eml() มีการโหลดเกินตัวอักษรที่ไม่มีค่าใช้จ่ายซึ่งจะกลับมา std::vector<std::uint8_t>, ที่เป็นประโยชน์สําหรับการสตรีมผลลัพธ์ไปยังการเชื่อมต่อเครือข่ายหรือ ฐานข้อมูลโดยไม่ต้องสัมผัสระบบไฟล์:

#include <filesystem>
#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"));

    // In-memory MSG bytes
    const auto msg_bytes = message.save();

    // In-memory EML bytes
    const auto eml_bytes = message.save_to_eml();
}

ปัญหาทั่วไปและวิธีแก้ไข

load_from_eml() ใส่ไฟล์ที่ดูถูกต้อง EML. ตรวจสอบไฟล์ที่ใช้ RFC 5322 EML ไฟล์ที่ส่งออกโดยบางเครื่องมืออาจใช้ LF เหล่านี้ แคปทอรี มีการคาดหวังว่าสายตรงตามมาตรฐาน.

การขาดแคลนหลังจากการแปลง EML → MSG. ตรวจสอบว่า EML มีมาตรฐาน MIME multipart/mixed หรือ multipart/related ส่วนประกอบการแนบ. รูปภาพในร่มอ้างอิง ทาง cid: มีไว้ในตัวยึดใน MSG และควรมีอยู่.

ร่างกาย HTML ที่หายไปหลังจาก MSG → EML. เนื้อหา HTML ถูกเก็บรักษาผ่านการเดินทางรอบ ๆ เมื่อ message.html_body() ไม่ว่างเปล่า หากเพียงแค่ message.body() เป็นที่อาศัยอยู่ใน แหล่งที่มา MSG EML จะมีส่วนหนึ่งของข้อความธรรมดาเท่านั้น.

save_to_eml() การส่งออกของ stream ว่างเปล่า. เปิดการส่งออกด้วย std::ios::binary.กระแสข้อความโหมดทําลาย MIME โครงสร้างใน Windows.

วัตถุที่ประกอบด้วย ?= การเข้ารหัส artefacts. นี่คือ RFC 2047 รหัสคําลําดับ EML ในอิสระ คลังเก็บไว้ตามที่อยู่ decode กับ RFC 2047 คลาสสิก หากคุณต้องการหัวข้อ Unicode ง่าย.

คำถามที่พบบ่อย

มีการเก็บรักษาแถบแนบในทั้งสองทิศทางของการแปลงหรือไม่?

ใช่ การแปลงทั้ง EML → MSG และ MSG→ EML นําเข้าทั้งหมดของ MIME โดยการส่งผ่านทาง mapi_message รายการที่แนบมา บีตไบนารี่จะแสดงเป็นตัวแทนดิบใน mapi_attachment::data.

EML → MSG รักษาร่างกาย HTML หรือไม่?

ใช่ ถ้า EML มี a text/html MIME ส่วนที่เก็บไว้ในร่างกาย MAPI HTML สิทธิ์ของทรัพย์สินและกลับจาก message.html_body() หลังจากโหลด.

ฉันสามารถแปลงชุดไดเรกทอรีของ EML ไฟล์ได้หรือไม่?

ใช่. Iterate directory กับ std::filesystem::directory_iterator และโทร mapi_message::load_from_eml() ในแต่ละ .eml ทางแล้วบันทึกด้วย save():

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

int main()
{
    for (const auto& entry : std::filesystem::directory_iterator("emails/"))
    {
        if (entry.path().extension() == ".eml")
        {
            auto msg = aspose::email::foss::msg::mapi_message::load_from_eml(entry.path());
            auto out = entry.path();
            out.replace_extension(".msg");
            msg.save(out);
        }
    }
}

สิ่งที่ได้รับการรักษาไว้ในระหว่าง EML ↔ MSG การแปลง?

ธีม ร่างกายข้อความธรรมดา HTML รอบ ชื่อผู้ส่ง และ อีเมล รายการผู้รับ และไฟล์แนบทั้งหมด MIME ป้าย, หัวหน้าการเดินทางนอกหัวลําเลียงพื้นฐานและข้อมูลที่เข้ารหัสด้วย TNEF ไม่ได้รับการเก็บรักษา.

เป็น load_from_eml() ทรานตู้?

ทุกโทรเพื่อ load_from_eml() สร้างอิสระ mapi_message กรณี คุณ สามารถเรียกมันจากหลายหัวข้อพร้อมกันจนกว่าแต่ละหัวนําใช้ของตัวเอง ข้อความวัตถุ.

นี่

 ภาษาไทย