C++ में एटैक्ट्स के साथ कैसे काम करें

C++ में एटैक्ट्स के साथ कैसे काम करें

Aspose.Email FOSS for C++ provides the mapi_attachment कक्षाएं बनाने के लिए अनुलग्नक कच्चे बूट या स्ट्रीम से, और 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() एक संदेश बनाने के दौरान बाइनरी डेटा जोड़ने के लिए।. फ़ाइल नाम प्रदान करें, कच्चे बाइट्स के रूप में एक 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 - अंतर्निहित संदेशों का पता लगाने और एक्सेस करने के लिए

कुछ एमएसजी फ़ाइलें अन्य एमएमजी फाइलों को एप्लिकेशन (प्रारंभ या बैठक के निमंत्रण) के रूप में शामिल करती हैं।. उपयोग करें 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. के लिए एलिप्शन हेडर एक अंतर्निहित एमएसजी का संकेत देता है, लेकिन डेटा पारदर्शी नहीं था. यह जांचें कि स्रोत एमएसजी वैध है और ट्रंक नहीं किया गया है।.

निकालने वाले अनुलग्नक को बचाना एक मार्ग त्रुटि के साथ विफल हो जाता है।. कुछ फ़िल्में फिलिमिंग लक्ष्य फ़ाइल सिस्टम पर अक्षरों को शामिल करें (उदाहरण के लिए,., :, ? पर Windows - स्वास्थ्य संबंधी attachment.filename उत्पादन मार्ग का निर्माण करने से पहले।.

बड़े एमएसजी फ़ाइलें उच्च स्मृति उपयोग का कारण बनती हैं।. -मैंने कहाmapi_message::from_file() सबको लोड बड़ी फ़ाइलों के लिए, सीएफबी कंटेनर को खोलने के साथ 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() एक स्वतंत्र एप्लिकेशन इनपुट जोड़ें. डुप्लिंग फ़ाइल नामों की अनुमति है; वे जोड़ सूची में स्थिति के अनुसार अलग किए जाते हैं।.

मैं इनलाइन छवियों के लिए सामग्री आईडी कैसे सेट करूं?

  1. सामग्री आईडी श्रृंखला को चौथा तर्क के रूप में पारित करें 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 को उत्पन्न नहीं होता है।.

क्या मैं बचत से पहले एक एप्लिकेशन हटा सकता हूं?

के लिए mapi_message API में एक सीधे हटाने-टिक के तरीके को प्रदर्शित नहीं करता है 0.1.0. Build a new message, copy the desired properties and attachments manually, और नए वस्तु को बचाने के लिए।.

देखें भी

 हिन्दी