Outlook MSG फ़ाइलों में अटैचमेंट्स प्रबंधित करें
Aspose.Email for C++ Microsoft Outlook MSG फ़ाइलों के साथ कार्य करते समय अटैचमेंट्स तक पहुँचने, सहेजने, हटाने और एम्बेड करने के लिए एक समृद्ध API प्रदान करता है। अटैचमेंट्स को इस माध्यम से संभाला जाता है MapiMessage क्लास, उसका उपयोग करके Attachments प्रॉपर्टी, जो एक MapiAttachmentCollection.
इस लेख में नमूने इन हेडर्स और नेमस्पेसेज़ का उपयोग करते हैं:
#include <MailMessage.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiAttachment.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <Mapi/MapiObjectProperty.h>
#include <Mapi/MapiPropertyCollection.h>
#include <Mapi/MailConversionOptions.h>
#include <Mapi/ReferenceAttachmentOptions.h>
#include <Mapi/InlineAttachmentExtractor.h>
#include <system/io/file.h>
#include <system/io/directory.h>
#include <system/io/path.h>
#include <system/io/memory_stream.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
MSG फ़ाइल से अटैचमेंट्स सहेजें
MSG फ़ाइल से अटैचमेंट्स निकालने और सहेजने के लिए:
- संदेश को लोड करें, उपयोग करके MapiMessage::Load.
- परिचालित करें MapiAttachmentCollection से लौटाया गया
get_Attachments. - प्रत्येक अटैचमेंट को उपयोग करके सहेजें MapiAttachment::Save() विधि।
SharedPtr<MapiMessage> message = MapiMessage::Load(u"MsgWithAtt.msg");
IO::Directory::CreateDirectory_(u"SavedAttachments");
for (SharedPtr<MapiAttachment> attachment : message->get_Attachments())
{
String path = IO::Path::Combine(u"SavedAttachments", attachment->get_LongFileName());
attachment->Save(path);
Console::WriteLine(u"Saved " + path);
}
अटैचमेंट्स हटाएँ
Aspose.Email for C++ दो स्थैतिक विधियां प्रदान करता है जो संदेश फ़ाइल के संलग्नकों को बिना कोड में खोलें हटाती हैं:
- RemoveAttachments संदेश फ़ाइल का पथ लेती है, संदेश को पार्स करती है और सभी संलग्नकों को हटा देती है। यह हटाए गए संलग्नकों को वापस करती है।
- DestroyAttachments MSG फ़ाइल को पूरी तरह पार्स किए बिना वही करता है, जिससे यह तेज़ हो जाता है।
दोनों विधियां फ़ाइल को स्थान पर बदलती हैं, इसलिए नमूना प्रतियों पर काम करता है।
IO::File::Copy(u"MsgWithAtt.msg", u"ToRemove.msg", true);
IO::File::Copy(u"MsgWithAtt.msg", u"ToDestroy.msg", true);
// parses the file and returns the attachments it removed
SharedPtr<MapiAttachmentCollection> removed = MapiMessage::RemoveAttachments(u"ToRemove.msg");
Console::WriteLine(u"Removed: " + Convert::ToString(removed->get_Count()));
// does the same without parsing the message, which is faster
MapiMessage::DestroyAttachments(u"ToDestroy.msg");
Console::WriteLine(u"Attachments left: " +
Convert::ToString(MapiMessage::Load(u"ToRemove.msg")->get_Attachments()->get_Count()) + u" / " +
Convert::ToString(MapiMessage::Load(u"ToDestroy.msg")->get_Attachments()->get_Count()));
MSG अटैचमेंट्स जोड़ें
MSG फ़ाइलें अन्य MSG फ़ाइलें मानक या एम्बेडेड संलग्नकों के रूप में रख सकती हैं। MapiAttachmentCollection::Add जो एक MapiMessage एक Outlook संदेश को एम्बेड करता है; Outlook फिर संलग्नक को फ़ाइल की बजाय संदेश के रूप में खोलता है।
निम्न कोड नमूना एक नया संदेश बनाता है, एक मौजूदा MSG फ़ाइल को एम्बेडेड संदेश के रूप में संलग्न करता है और परिणाम सहेजता है।
SharedPtr<MapiMessage> message = MakeObject<MapiMessage>(u"from@test.com", u"to@test.com", u"Subj", u"This is a message body");
SharedPtr<MapiMessage> attachMsg = MapiMessage::Load(u"Message.msg");
message->get_Attachments()->Add(u"Weekly report.msg", attachMsg);
message->Save(u"WithEmbeddedMsg_out.msg");
SharedPtr<MapiAttachment> attachment = MapiMessage::Load(u"WithEmbeddedMsg_out.msg")->get_Attachments()->idx_get(0);
Console::WriteLine(attachment->get_DisplayName() + u" is an Outlook message: " + Convert::ToString(attachment->get_ObjectData()->get_IsOutlookMessage()));
संलग्नक प्रकारों को अलग करें
Outlook संदेश के प्रत्येक संलग्नक को अटैचमेंट्स संग्रह, लेकिन सभी एक ही प्रकार के नहीं होते। IsInline फ़ाइलों को चिन्हित करता है जिन्हें बॉडी स्थान पर प्रदर्शित करती है - आम तौर पर छवियां - और IsReference वे जो केवल कहीं और रखी फ़ाइल का लिंक होते हैं।
SharedPtr<MapiMessage> message = MapiMessage::Load(u"messageWithEmbeddedEML.msg");
for (SharedPtr<MapiAttachment> attach : message->get_Attachments())
{
Console::WriteLine(attach->get_DisplayName() +
u" | inline: " + Convert::ToString(attach->get_IsInline()) +
u" | reference: " + Convert::ToString(attach->get_IsReference()) +
u" | file name: " + attach->get_LongFileName());
}
फ़ाइलें, संदेश और संदर्भ जोड़ें
MapiAttachmentCollection::Add के लिए प्रत्येक प्रकार के संलग्नक का एक ओवरलोड है: बाइट ऐरे एक सामान्य फ़ाइल बन जाता है, एक MapiMessage एक एम्बेडेड संदेश बन जाता है, और एक ReferenceAttachmentOptions ऑब्जेक्ट क्लाउड स्टोरेज में फ़ाइल का लिंक बनाता है - साझा लिंक, फ़ाइल का स्थान और प्रदाता का नाम।
SharedPtr<MapiMessage> message = MakeObject<MapiMessage>(u"from@test.com", u"to@test.com",
u"Subj", u"This is a message body");
// a regular attachment from a byte array
message->get_Attachments()->Add(u"note.txt", IO::File::ReadAllBytes(u"description.html"));
// another Outlook message, embedded as an attachment
SharedPtr<MapiMessage> attachMsg = MapiMessage::Load(u"Message.msg");
message->get_Attachments()->Add(u"Weekly report.msg", attachMsg);
// a reference attachment: the file stays in the cloud storage
SharedPtr<ReferenceAttachmentOptions> options = MakeObject<ReferenceAttachmentOptions>(
u"https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
u"https://drive.google.com/drive/my-drive",
u"GoogleDrive");
message->get_Attachments()->Add(u"Document.pdf", options);
message->Save(u"WithAttachments.msg");
Console::WriteLine(u"Attachments: " + Convert::ToString(message->get_Attachments()->get_Count()));
एक एम्बेडेड संदेश पढ़ें
एक एम्बेडेड संदेश की सामग्री के द्वारा उजागर किया जाता है ObjectData. IsOutlookMessage बताता है कि ऑब्जेक्ट वास्तव में एक संदेश है या नहीं, ToMapiMessage इसे लौटाता है, और MapiMessage::FromProperties जब आपको आगे बदलना हो तो यह कच्ची प्रॉपर्टी कलेक्शन से पुनः बनाता है।
SharedPtr<MapiMessage> message = MapiMessage::Load(u"WithEmbeddedMsg.msg");
SharedPtr<MapiAttachment> attachment = message->get_Attachments()->idx_get(0);
if (attachment->get_ObjectData() != nullptr && attachment->get_ObjectData()->get_IsOutlookMessage())
{
// the embedded item as a MapiMessage
SharedPtr<MapiMessage> embedded = attachment->get_ObjectData()->ToMapiMessage();
Console::WriteLine(u"Embedded subject: " + embedded->get_Subject());
// the same item built from its properties and converted to MIME
SharedPtr<MapiMessage> fromProperties = MapiMessage::FromProperties(attachment->get_ObjectData()->get_Properties());
SharedPtr<MailMessage> eml = fromProperties->ToMailMessage(MakeObject<MailConversionOptions>());
eml->Save(u"NestedMailMessage.eml", SaveOptions::get_DefaultEml());
}
संलग्नक डालें, बदलें और पुनःनाम दें
ऐड करने के अलावा, संग्रह एक संदेश को चुनी हुई स्थिति में रख सकता है Insert और एक मौजूदा प्रविष्टि को ओवरराइट करें बदलेँ. Outlook में दिखाया गया नाम लिखने योग्य DisplayName प्रॉपर्टी।
SharedPtr<MapiMessage> message = MapiMessage::Load(u"WithAttachments.msg");
// take one of the embedded messages out
SharedPtr<IO::MemoryStream> memoryStream = MakeObject<IO::MemoryStream>();
message->get_Attachments()->idx_get(1)->Save(memoryStream);
memoryStream->set_Position(0);
SharedPtr<MapiMessage> extracted = MapiMessage::Load(memoryStream);
// put it back at a chosen position
message->get_Attachments()->Insert(1, u"inserted.msg", extracted);
// or overwrite the attachment that is already there
message->get_Attachments()->Replace(1, u"replaced.msg", extracted);
// the display name can be changed afterwards
message->get_Attachments()->idx_get(0)->set_DisplayName(u"New display name");
message->Save(u"RearrangedAttachments.msg");
for (SharedPtr<MapiAttachment> attach : MapiMessage::Load(u"RearrangedAttachments.msg")->get_Attachments())
{
Console::WriteLine(attach->get_DisplayName());
}
Replace एक मौजूदा प्रविष्टि को ओवरराइट करता है; यह संग्रह के अंत से आगे के इंडेक्स पर नहीं बना सकता।
oledata.mso से OLE ऑब्जेक्ट निकालें
जब संदेश का बॉडी HTML होता है न कि RTF, तो Outlook उसके OLE ऑब्जेक्ट को अलग संलग्नक के रूप में नहीं रखता: वे एक ही oledata.mso अटैचमेंट। InlineAttachmentExtractor::EnumerateMsoPackage पैकेज को व्यक्तिगत OLE आइटम में विभाजित करता है। प्रत्येक आइटम एक Microsoft कम्पाउंड डॉक्यूमेंट है; दस्तावेज़ को पढ़ना - आमतौर पर उसकी CONTENTS स्ट्रीम - यह कम्पाउंड-फ़ाइल लाइब्रेरी का काम है, जिसे Aspose.Email शामिल नहीं करता।
SharedPtr<MapiMessage> msg = MapiMessage::Load(u"messageWithEmbeddedEML.msg");
for (SharedPtr<MapiAttachment> attachment : msg->get_Attachments())
{
if (attachment->get_LongFileName() == u"oledata.mso")
{
// every entry is one OLE package in Compound File format
auto packages = InlineAttachmentExtractor::EnumerateMsoPackage(
MakeObject<IO::MemoryStream>(attachment->get_BinaryData()));
for (auto package : packages)
{
Console::WriteLine(package.get_Key() + u": " +
Convert::ToString(package.get_Value()->get_Length()) + u" bytes");
// hand the bytes to a library that reads Compound File documents
IO::File::WriteAllBytes(package.get_Key(), package.get_Value());
}
}
}
नए जोड़े गए अटैचमेंट तक पहुँच
यह Add के मेथड्स MapiAttachmentCollection क्लास लौटाता है MapiAttachment ऑब्जेक्ट वे बनाते हैं। यह आपको नए अटैचमेंट तक तुरंत पहुँच देता है, ताकि आप उसे डालते ही कॉन्फ़िगर कर सकें बिना कलेक्शन में खोजे। निम्न ओवरलोड्स बनाए गए अटैचमेंट को लौटाते हैं:
Add(String name, ArrayPtr<uint8_t> data)- बाइट ऐरे से एक सामान्य संलग्नक जोड़ता है।Add(String name, SharedPtr<MapiMessage> msg)- एक Outlook संदेश को एम्बेडेड संलग्नक के रूप में जोड़ता है।Add(String name, SharedPtr<ReferenceAttachmentOptions> options)- एक संदर्भ संलग्नक जोड़ता है।
निम्न कोड नमूना एक सामान्य संलग्नक और एक एम्बेडेड संदेश जोड़ता है और उनके डिस्प्ले नाम बदलता है set_DisplayName():
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiAttachment.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Email::Mapi;
auto message = MakeObject<MapiMessage>(u"from@domain.com", u"to@domain.com", u"Weekly report", u"Please find the files attached.");
// Add a regular attachment and change its display name
SharedPtr<MapiAttachment> attachment = message->get_Attachments()->Add(u"invoice.pdf", IO::File::ReadAllBytes(u"invoice.pdf"));
attachment->set_DisplayName(u"Invoice #2026-001.pdf");
// Add an embedded message and change its display name
SharedPtr<MapiAttachment> embedded = message->get_Attachments()->Add(u"original.msg", MapiMessage::Load(u"message.msg"));
embedded->set_DisplayName(u"Original request");
message->Save(u"report.msg");