วิธีการทํางานกับแถบแนบใน C++

วิธีการทํางานกับแถบแนบใน C++

Aspose.Email FOSS for C++ provides the mapi_attachment หลักสูตรสําหรับการสร้าง attachments จากไบต์ดิบหรือสตรีมและ mapi_message รายการที่แนบมาสําหรับการอ่านแถลง จากไฟล์ MSG ที่อยู่ คุณสามารถเพิ่มแถลงในขณะที่เขียนข้อความและตรวจสอบพวกเขา เมื่อโหลดไฟล์รวมถึงการตรวจจับว่าแถบแนบเป็นตัวยึดในตัวหรือไม่ MSG ข้อความ.

ขั้นตอนที่ 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 — เพิ่มไฟล์แนบจากไทเท่

ใช้ mapi_message::add_attachment() เพื่อเพิ่มข้อมูลไบนารีเมื่อเขียนข้อความ. ให้ชื่อไฟล์ บิตดิบเป็น a std::vector<std::uint8_t>,และ MIME ประเภทเนื้อหา:

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

int main()
{
    auto message = aspose::email::foss::msg::mapi_message::create(
        "Report", "Please review the attached report.");
    message.set_sender_name("Alice");
    message.set_sender_email_address("alice@example.com");
    message.add_recipient("bob@example.com", "Bob");

    // Attach a text file
    std::vector<std::uint8_t> text_data{'H', 'e', 'l', 'l', 'o'};
    message.add_attachment("notes.txt", text_data, "text/plain");

    // Attach a binary blob
    std::vector<std::uint8_t> bin_data{0x00, 0x01, 0x02, 0x03};
    message.add_attachment("data.bin", bin_data, "application/octet-stream");

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

ขั้นตอนที่ 3 — เพิ่มไฟล์แนบจากไฟล์บนดิสก์

อ่านไฟล์เป็นตัวบิตเวกเตอร์แล้วส่งไปยัง add_attachment():

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

int main()
{
    // Read a file into bytes
    std::ifstream file("document.pdf", std::ios::binary);
    std::vector<std::uint8_t> pdf_bytes(
        std::istreambuf_iterator<char>(file),
        std::istreambuf_iterator<char>{});

    auto message = aspose::email::foss::msg::mapi_message::create(
        "Document", "See attached PDF.");
    message.set_sender_name("Alice");
    message.set_sender_email_address("alice@example.com");
    message.add_recipient("bob@example.com", "Bob");
    message.add_attachment("document.pdf", pdf_bytes, "application/pdf");

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

ขั้นตอนที่ 4 — อ่านไฟล์แนบจาก MSG ไฟล์ที่มีอยู่

message.attachments() กลับไป A const การอ้างอิงไปยังเวกเตอร์ของวัตถุที่แนบมา. ทุกรายการ filename, mime_type, data (ตัวบิตดิบ) และ content_id:

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

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

    std::cout << "Attachments: " << message.attachments().size() << '\n';
    for (std::size_t i = 0; i < message.attachments().size(); ++i)
    {
        const auto& a = message.attachments()[i];
        std::cout << "[" << (i + 1) << "]"
                  << "  name="         << a.filename
                  << "  mime="         << a.mime_type
                  << "  bytes="        << a.data.size()
                  << "  content_id="   << a.content_id
                  << '\n';
    }
}

ขั้นตอนที่ 5 — ค้นหาและเข้าถึงข้อความในตัว

ไฟล์ MSG บางส่วนมีไฟล์อื่น ๆ MSG เป็นไฟล์แนบ (การส่งต่อหรือคําเชิญประชุม). ใช้ is_embedded_message() เพื่อตรวจจับสิ่งเหล่านี้และเข้าถึงข้อความที่สกปรกผ่านทาง embedded_message:

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

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

    for (const auto& attachment : message.attachments())
    {
        if (attachment.is_embedded_message() && attachment.embedded_message != nullptr)
        {
            const auto& inner = *attachment.embedded_message;
            std::cout << "Embedded subject: " << inner.subject() << '\n';
            std::cout << "Embedded from:    " << inner.sender_email_address() << '\n';

            // Save the embedded message as a standalone MSG
            inner.save(std::filesystem::path("embedded.msg"));
        }
        else
        {
            std::cout << "Regular attachment: " << attachment.filename
                      << " (" << attachment.data.size() << " bytes)\n";
        }
    }
}

ขั้นตอนที่ 6 - บันทึกข้อมูลแถบเข้าสู่ไดรฟ์

สกัดบิตไฟล์แนบและเขียนลงในไฟล์โดยใช้กระแสไฟล์ C++ มาตรฐาน:

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

    const std::filesystem::path out_dir("attachments");
    std::filesystem::create_directories(out_dir);

    for (const auto& a : message.attachments())
    {
        if (!a.is_embedded_message())
        {
            const auto out_path = out_dir / a.filename;
            std::ofstream out(out_path, std::ios::binary);
            out.write(reinterpret_cast<const char*>(a.data.data()),
                      static_cast<std::streamsize>(a.data.size()));
            std::cout << "Saved: " << out_path << '\n';
        }
    }
}

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

attachment.data เป็นที่ว่างเปล่าสําหรับความเชื่อมโยงที่มีขนาดใหญ่. ตรวจสอบไฟล์แหล่งข้อมูล MSG มีข้อมูลที่แนบมา (ไม่ใช่ลิงก์ OLE) MSG ไฟล์ส่งออกจากบางส่วนของ ลูกค้าอีเมลอาจเก็บไฟล์แนบเป็นลิงก์อ้างอิงภายนอกที่สมุดสามารถติดตามได้.

is_embedded_message() กลับมา true แต่ embedded_message เป็น nullptr. โซฟา หัวหน้าไฟล์ที่แนบมาแสดงว่า MSG อยู่ในตัว แต่ข้อมูลนั้นไม่สามารถอ่านได้ ตรวจสอบให้แน่ใจว่า แหล่งที่มา MSG เป็นถูกต้องและไม่ได้ตัดออก.

การบันทึกการแนบมาที่ถูกลบไม่ได้ด้วยข้อผิดพลาดทางเดิน. ไฟล์บางส่วน มีตัวอักษรที่ไม่ถูกต้องในระบบไฟล์เป้าหมาย (เช่น ตัวอธิบายการใช้งาน)., :, ? สะพาน หน้าต่าง) การดูแลสุขภาพ attachment.filename ก่อนที่จะสร้างเส้นทางการส่งออก.

ไฟล์ขนาดใหญ่ MSG ทําให้ใช้หน่วยความจําสูง. สารสกัดmapi_message::from_file() โหลดทั้งหมด ข้อมูลแนบมาในหน่วยความจํา สําหรับไฟล์ขนาดใหญ่เปิดภาชนะ CFB ที่อยู่เบื้องต้นด้วย cfb_reader เพื่อเข้าถึงสตรีมแต่ละรายโดยไม่ต้องมีเอกลักษณ์อย่างเต็มที่ของพาร์ทิชันทั้งหมด.

add_attachment() ไม่พบในเวลาการประกอบ. ตรวจสอบให้แน่ใจว่าคุณใช้ที่ถูกต้อง รวมถึง: "aspose/email/foss/msg/mapi_message.hpp".วิธีการเป็นสมาชิกของ aspose::email::foss::msg::mapi_message.

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

MIME ชนิดใดที่ฉันควรใช้สําหรับแถบแนบ?

ใช้มาตรฐาน IANA MIME ประเภทสําหรับรูปแบบไฟล์: "text/plain" สําหรับ .txt, "application/pdf" สําหรับ .pdf, "application/octet-stream" เป็นการตกแต่งทั่วไปสําหรับ ไฟล์ไบนารี ประเภท MIME จะถูกจัดเก็บเป็น attach_mime_tag MAPI คุณสมบัติ.

ฉันสามารถแนบไฟล์เดียวกันหลายครั้งได้หรือไม่?

ใช่ แต่ละโทรให้ add_attachment() แพคเกจที่แนบมาเป็นตัวยึดแบบอิสระ คู่มือการพิมพ์ ชื่อไฟล์ที่อนุญาต; พวกเขาจะแยกออกจากตําแหน่งในรายการแถลง.

ฉันจะตั้งค่า Content-ID สําหรับภาพในบรรทัดฐานได้อย่างไร?

ส่งรหัสเนื้อหาเป็นข้อห้ามที่สี่ไปยัง mapi_attachment::from_stream():

auto att = aspose::email::foss::msg::mapi_attachment::from_bytes(
    "logo.png", png_bytes, "image/png", "<logo-cid@example.com>");

content_id มีการแสดงออกเป็น attachment.content_id เมื่ออ่านกลับ.

เป็น attachment.data คัดลอกหรืออ้างอิง?

attachment.data เป็น a std::vector<std::uint8_t> บันทึกโดยค่าภายใน mapi_message object การเข้าถึงมันไม่ได้เปิดใช้งาน I / O เพิ่มเติม.

ฉันสามารถลบไฟล์แนบมาก่อนที่จะบันทึกได้หรือไม่?

โซฟา mapi_message API ไม่แสดงวิธีการถอดแนบโดยตรงในรุ่น 0.1.0. Build a new message, copy the desired properties and attachments manually, และบันทึกวัตถุใหม่.

นี่

 ภาษาไทย