כיצד לעבוד עם קבצים מצורפים ב C++

כיצד לעבוד עם קבצים מצורפים ב C++

Aspose.Email FOSS for C++ provides the mapi_attachment שיעורים ליצירת תוספים מתוך אובייטים גלויים או זרמים, ואת mapi_message רשימת הצעות קריאה לצרכים הקשורים מ- 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 – הוסף קובץ מצורף מ-Bytes

שימוש mapi_message::add_attachment() להוסיף נתונים בינאריים בעת יצירת הודעה. הגדרת שם הקובץ, האגדות המרובות כ-a 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() חזרה 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. The כותרת ההקבלה מציינת MSG המוטבע, אך הנתונים לא היו נבחרים. המקור הוא חוקי ולא מופרך.

שמירת קבצים מצורפים נכשלת עם שגיאה במסלול. מספר קבצים קשורים מכיל תווים שאינם חוקיים במערכת הקבצים היעד (למשל,., :, ? הוא ספא - Sanitise attachment.filename לפני שמתחילים לבנות את מסלול ההפעלה.

קבצים גדולים גורמים לשימוש גבוה בזיכרון.mapi_message::from_file() לואיס All קבצים גדולים, פתח את מיכל 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" כמו Fallback for קבצים בינאריים. MIME סוג מאוחסן כ- attach_mime_tag רכושו של הנכס.

האם ניתן להוסיף את אותו קובץ מספר פעמים?

כל שיחה – לכל שיחת add_attachment() כניסה למאמר עצמאי - עותק שמות קבצים מותרים; הם נבדלים על פי מיקום ברשימת הקבצי המצורפים.

כיצד ניתן להגדיר את Content-ID עבור תמונות מקוונות?

העבירו את שורת ה-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 גישה אליו אינה מעוררת I/O נוסף.

האם ניתן להסיר קובץ מצורף לפני שמירה?

The mapi_message API אינו חושף שיטה של הסרת קבצים ישירה בגרסה 0.1.0. Build a new message, copy the desired properties and attachments manually, שמור את האובייקט החדש.

ראה גם

 עברית