كيفية العمل مع الارتباطات في C++

كيفية العمل مع الارتباطات في C++

Aspose.Email FOSS for C++ provides the mapi_attachment فئة لإنشاء الارتباطات من البيوت الخام أو التدفقات، و mapi_message قائمة المرفقات لقراءة المضافات من ملفات MSG الحالية.يمكنك إضافة المرفقات عند تكوين الرسائل والتحقق منها عند تحميل الملفات، بما في ذلك اكتشاف ما إذا كان ملحق نفسه هو مكون مدمج. رسالة MSG .

الخطوة الأولى: إعداد المشروع

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() إرفاق البيانات الثنائية عند تكوين رسالة. إعطاء اسم الفيل ، البيوت الخام كأ std::vector<std::uint8_t>,وَالْمُؤْنِينَ مِنْ نوع المحتوى :

#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() عودة أ 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 تحتوي على ملفات MSM الأخرى كملفات مرفقة (مكثفة أو دعوة للقاء). استخدام 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). يمكن لعملاء البريد إخراج المرفقات كمرجع خارجي لا يستطيع مكتبة متابعته.

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 خريطة الملكية .

هل يمكنني إرفاق نفس الملف عدة مرات؟?

نعم، كل دعوة إلى add_attachment() إدخال ملصق مستقل.Duplicate تسمح أسماء الفيل؛ يتم تمييزها حسب الموقع في قائمة المرفقات.

كيف أضع علامة المحتوى 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 هو أ std::vector<std::uint8_t> يتم تخزينها من خلال القيمة داخل mapi_message الوصول إليه لا يسبب إضافية I/O.

هل يمكنني إزالة ملصق قبل الادخار؟?

وَالْمُتَّقِينَ mapi_message API لا يعرض طريقة إزالة المرفقات المباشرة في النسخة 0.1.0. Build a new message, copy the desired properties and attachments manually, وخلص من الكائن الجديد.

انظر أيضا

 العربية