अटैचमेंट और एम्बेडेड ऑब्जेक्ट्स के साथ कार्य करना

अटैचमेंट्स के साथ काम करना

Email attachments वे फ़ाइलें हैं जो ईमेल संदेश के साथ भेजी जाती हैं। इन फ़ाइलों में दस्तावेज़, चित्र, संग्रह आदि शामिल हो सकते हैं।

Aspose.Email for C++ में अटैचमेंट्स को इस द्वारा प्रबंधित किया जाता है Attachment क्लास, जो साथ मिलकर काम करता है MailMessage क्लास। आप प्रोग्रामेटिक रूप से अटैचमेंट्स को जोड़, हटाए, निकाल या प्रदर्शित कर सकते हैं।

Note: अधिकतम अटैचमेंट आकार मेल सर्वर पर निर्भर करता है (उदाहरण के लिए, Gmail अटैचमेंट्स को 10 MB तक सीमित करता है)।

इस लेख में नमूने इन हेडर्स और नेमस्पेसेज़ का उपयोग करते हैं:

#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <Attachment.h>
#include <AttachmentCollection.h>
#include <ReferenceAttachment.h>
#include <AttachmentProviderType.h>
#include <AttachmentPermissionType.h>
#include <AlternateView.h>
#include <AlternateViewCollection.h>
#include <LinkedResource.h>
#include <LinkedResourceCollection.h>
#include <EmlLoadOptions.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <MsgSaveOptions.h>
#include <HtmlSaveOptions.h>
#include <ResourceHtmlRenderingEventArgs.h>
#include <Mime/MediaTypeNames.h>
#include <Mime/ContentType.h>
#include <Mime/HeaderCollection.h>
#include <system/io/file.h>
#include <system/io/directory.h>
#include <system/io/path.h>
#include <system/convert.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mime;

ईमेल में अटैचमेंट जोड़ना

ईमेल संदेश में एक या अधिक फ़ाइलों को अटैच करने के लिए नीचे दिए गए चरणों का पालन करें:

  1. का एक इंस्टेंस बनाएं MailMessage क्लास।
  2. Create Attachment प्रत्येक फ़ाइल के लिए ऑब्जेक्ट्स।
  3. अटैचमेंट को संदेश में जोड़ें - इसके माध्यम से अटैचमेंट्स संग्रह या साथ में AddAttachment.
  4. यदि आवश्यक हो तो संदेश सहेजें।
SharedPtr<MailMessage> message = MakeObject<MailMessage>();
message->set_From(u"sender@sender.com");
message->get_To()->Add(u"receiver@gmail.com");
message->set_Subject(u"Files for the review");

// an Attachment is created from a file; its name and content type come from the file
message->get_Attachments()->Add(MakeObject<Attachment>(u"description.html"));
message->AddAttachment(MakeObject<Attachment>(u"1.jpg"));
message->AddAttachment(MakeObject<Attachment>(u"portrait.jpg"));

message->Save(u"WithAttachments.msg", SaveOptions::get_DefaultMsgUnicode());
Console::WriteLine(u"Attachments: " + Convert::ToString(message->get_Attachments()->get_Count()));

ईमेल से अटैचमेंट हटाना

आप संदेश से अटैचमेंट्स को आसानी से हटाने के लिए इसका उपयोग कर सकते हैं Remove() विधि अटैचमेंट्स संग्रह।

निम्न कोड स्निपेट दर्शाता है कि अटैचमेंट को कैसे हटाया जाए:

  1. लोड या बनाएँ एक MailMessage इंस्टैंस।
  2. हटाने के लिए अटैचमेंट खोजें - वह ऑब्जेक्ट रखें जो आपने जोड़ा था, या इसे संग्रह में खोजें।
  3. इसे पास करें Remove(); RemoveAt() स्थिति पर अटैचमेंट हटाता है।
  4. वैकल्पिक रूप से, शेष अटैचमेंट दिखाएँ।
SharedPtr<MailMessage> message = MakeObject<MailMessage>();
message->set_From(u"sender@sender.com");
message->get_To()->Add(u"receiver@gmail.com");

SharedPtr<Attachment> description = MakeObject<Attachment>(u"description.html");
message->get_Attachments()->Add(description);
message->AddAttachment(MakeObject<Attachment>(u"1.jpg"));
message->AddAttachment(MakeObject<Attachment>(u"portrait.jpg"));

// remove one attachment and save the message
message->get_Attachments()->Remove(description);
message->Save(u"AttachmentRemoved.msg", SaveOptions::get_DefaultMsgUnicode());

for (SharedPtr<Attachment> attachment : message->get_Attachments())
{
    Console::WriteLine(u"Left: " + attachment->get_Name());
}

अटैचमेंट फ़ाइल नाम प्रदर्शित करना

यह Name अटैचमेंट का नाम वह फ़ाइल नाम है जो संदेश देता है। संग्रह पर लूप सभी को सूचीबद्ध करता है:

SharedPtr<MailMessage> message = MailMessage::Load(u"WithAttachments.msg");

for (SharedPtr<Attachment> attachment : message->get_Attachments())
{
    Console::WriteLine(attachment->get_Name());
}

ईमेल से अटैचमेंट निकालना

एक मौजूदा ईमेल फ़ाइल से अटैचमेंट्स को निकालने और सहेजने के लिए नीचे दिए गए चरणों का पालन करें:

  1. संदेश को लोड करें इस MailMessage क्लास।
  2. के माध्यम से लूप करें अटैचमेंट्स संग्रह।
  3. प्रत्येक अटैचमेंट को डिस्क पर सहेजें Attachment::Save और वैकल्पिक रूप से उसका नाम दिखाएँ।

निम्नलिखित कोड स्निपेट संदेश के प्रत्येक अटैचमेंट को एक फ़ोल्डर में सहेजता है, प्रत्येक को उसके अपने नाम से:

SharedPtr<MailMessage> message = MailMessage::Load(u"WithAttachments.msg");

IO::Directory::CreateDirectory_(u"ExtractedAttachments");
for (SharedPtr<Attachment> attachment : message->get_Attachments())
{
    attachment->Save(IO::Path::Combine(u"ExtractedAttachments", attachment->get_Name()));
    Console::WriteLine(u"Saved " + attachment->get_Name());
}

|ईमेल में निकाले गए अटैचमेंट्स| | :- | |todo:image_alt_text|

अटैचमेंट कंटेंट डिस्क्रिप्शन पढ़ना

एक अटैचमेंट अपने स्वयं के MIME हेडर रखता है। वैकल्पिक Content-Description हेडर फ़ाइल का संक्षिप्त विवरण रखता है; यह पढ़ा जाता है Headers अटैचमेंट के संग्रह। नमूना हेडर लिखता है और इसे सहेजे गए संदेश से पढ़ता है।

SharedPtr<MailMessage> source = MakeObject<MailMessage>(u"sender@sender.com", u"receiver@gmail.com", u"Report", u"See the attachment.");
SharedPtr<Attachment> report = MakeObject<Attachment>(u"description.html");
report->get_Headers()->Add(u"Content-Description", u"Quarterly report");
source->get_Attachments()->Add(report);
source->Save(u"WithDescription.eml", SaveOptions::get_DefaultEml());

SharedPtr<MailMessage> message = MailMessage::Load(u"WithDescription.eml");
String description = message->get_Attachments()->idx_get(0)->get_Headers()->Get(u"Content-Description");
Console::WriteLine(u"Content-Description: " + description);

एम्बेडेड ऑब्जेक्ट्स के साथ काम करना

एक embedded object वह फ़ाइल है जो एक एप्लिकेशन में बनाई गई है और दूसरे दस्तावेज़ या ईमेल के भीतर संलग्न की गई है। उदाहरण के लिए, एक Microsoft Excel स्प्रेडशीट को Word दस्तावेज़ में एंबेड किया जा सकता है, या एक इमेज को सीधे ईमेल बॉडी में एंबेड किया जा सकता है।

जब कोई ऑब्जेक्ट एंबेड किया जाता है (केवल अटैच या लिंक नहीं), तो वह अपना मूल फॉर्मेट बनाए रखता है और उसके मूल एप्लिकेशन में खोला या संपादित किया जा सकता है।

ईमेल में ऑब्जेक्ट्स को एम्बेड करना

ईमेल संदेश में इमेज या कोई अन्य ऑब्जेक्ट एंबेड करने के लिए इसका उपयोग करें LinkedResource क्लास के साथ MailMessage और AlternateView क्लासेज़। यह आपको इनलाइन इमेज या मीडिया कंटेंट शामिल करने की अनुमति देता है जो सीधे संदेश बॉडी में दिखाई देता है।

निम्न कोड नमूना दिखाता है कि कैसे साधारण टेक्स्ट और HTML सामग्री दोनों वाले ईमेल संदेश को बनाया जाए, जिसमें एंबेडेड इमेज शामिल हो:

  1. का एक इंस्टेंस बनाएं MailMessage क्लास।
  2. प्रेषक, प्राप्तकर्ता और विषय फ़ील्ड सेट करें।
  3. इसका उपयोग करके एक साधारण‑टेक्स्ट बॉडी बनाएँ AlternateView.
  4. उपरोक्त क्लास का उपयोग करके एंबेडेड ऑब्जेक्ट के रेफ़रेंस के साथ एक HTML बॉडी बनाएँ cid (Content-ID). रेफ़रेंस को दोहरे उद्धरण में लिखें, src="cid:barcode": इस रूप में एक प्रोग्राम जो बाद में संदेश को लोड करता है, छवि को इनलाइन संसाधन के रूप में पहचानता है न कि अटैचमेंट के रूप में।
  5. का एक इंस्टेंस बनाएं LinkedResource क्लास और ऑब्जेक्ट लोड करें।
  6. लिंक्ड रिसोर्स को Content-ID असाइन करें।
  7. लिंक्ड रिसोर्स और वैकल्पिक व्यूज को संदेश में जोड़ें।
  8. ईमेल संदेश को सहेजें या भेजें।
SharedPtr<MailMessage> mail = MakeObject<MailMessage>();
mail->set_From(MakeObject<MailAddress>(u"test001@gmail.com"));
mail->get_To()->Add(u"test001@gmail.com");
mail->set_Subject(u"This is an email");

// the plain text part, for clients that do not display HTML
SharedPtr<AlternateView> plainView = AlternateView::CreateAlternateViewFromString(u"This is my plain text content", nullptr, u"text/plain");

// the HTML part refers to the image by its Content-ID: src="cid:barcode" maps to the resource with the ID "barcode"
SharedPtr<AlternateView> htmlView = AlternateView::CreateAlternateViewFromString(u"Here is an embedded image.<img src=\"cid:barcode\">", nullptr, u"text/html");

SharedPtr<LinkedResource> barcode = MakeObject<LinkedResource>(u"1.jpg", Mime::MediaTypeNames::Image::Jpeg);
barcode->set_ContentId(u"barcode");

mail->get_LinkedResources()->Add(barcode);
mail->get_AlternateViews()->Add(plainView);
mail->get_AlternateViews()->Add(htmlView);

mail->Save(u"EmbeddedImage.eml", SaveOptions::get_DefaultEml());

Note: > आप कई ऑब्जेक्ट्स एंबेड कर सकते हैं, लेकिन अधिकतम संदेश आकार आपके मेल सर्वर द्वारा सीमित होता है। उदाहरण के लिए, Gmail संदेश आकार को 10 MB तक सीमित करता है।

ईमेल से एंबेडेड ऑब्जेक्ट्स निकालना

आप मौजूदा ईमेल संदेश से एम्बेडेड ऑब्जेक्ट्स, जैसे छवियों को भी निकाल सकते हैं। वे अटैचमेंट नहीं हैं: लोड किया गया संदेश उन्हें अपने LinkedResources संग्रह में लोड करें। प्रक्रिया अटैचमेंट निकालने के समान है:

  1. ईमेल फ़ाइल को एक MailMessage.
  2. लिंक्ड संसाधनों के माध्यम से इटरेट करें।
  3. प्रत्येक एम्बेडेड ऑब्जेक्ट को डिस्क पर सहेजें; इसका Content-ID बताता है कि वह HTML बॉडी में किस रेफ़रेंस से संबंधित है।
  4. वैकल्पिक रूप से कंसोल में फ़ाइल नाम प्रदर्शित करें।
SharedPtr<MailMessage> message = MailMessage::Load(u"EmbeddedImage.eml");

IO::Directory::CreateDirectory_(u"EmbeddedObjects");
int32_t index = 0;
for (SharedPtr<LinkedResource> resource : message->get_LinkedResources())
{
    String fileName = IO::Path::Combine(u"EmbeddedObjects", u"object_" + Convert::ToString(index++) + u".jpg");
    resource->Save(fileName);
    Console::WriteLine(u"cid:" + resource->get_ContentId() + u" -> " + fileName);
}

|ईमेल में निकाले गए एम्बेडेड ऑब्जेक्ट्स| | :- | |todo:image_alt_text|

अटैचमेंट के साथ काम करने के अधिक तरीके

रेफ़रेंस अटैचमेंट जोड़ें

एक रेफ़रेंस अटैचमेंट संदेश के साथ यात्रा नहीं करता: यह साझा स्थान में फ़ाइल के लिंक को ले जाता है, इसलिए प्राप्तकर्ता हमेशा वर्तमान संस्करण खोलते हैं और संदेश स्वयं छोटा रहता है। ReferenceAttachment URI लेता है, जबकि ProviderType स्टोरेज सेवा का नाम बताता है और PermissionType प्राप्तकर्ता जो पहुंच प्राप्त करते हैं।

SharedPtr<MailMessage> eml = MailMessage::Load(u"sample.eml");

// the message carries a link to the file, not the file itself
SharedPtr<ReferenceAttachment> refAttach = MakeObject<ReferenceAttachment>(u"https://example.com/shared/Document.docx");
refAttach->set_Name(u"Document.docx");
refAttach->set_ProviderType(AttachmentProviderType::OneDrivePro);
refAttach->set_PermissionType(AttachmentPermissionType::AnyoneCanEdit);

eml->get_Attachments()->Add(refAttach);
eml->Save(u"WithReferenceAttachment.eml", SaveOptions::get_DefaultEml());

// a reference attachment is recognized when the message is read back
SharedPtr<MailMessage> reloaded = MailMessage::Load(u"WithReferenceAttachment.eml");
for (SharedPtr<Attachment> attachment : reloaded->get_Attachments())
{
    Console::WriteLine(attachment->get_Name() + u" - IsUri: " + Convert::ToString(attachment->get_IsUri()));
}

एम्बेडेड संदेश और हस्ताक्षरित सामग्री की पहचान करें

IsEmbeddedMessage बताता है कि अटैचमेंट स्वयं एक संदेश है या नहीं; ऐसे अटैचमेंट को MailMessage::Load सीधे इसकी सामग्री स्ट्रीम से। एक हस्ताक्षरित संदेश एक अलग मामला है: यह एकल smime.p7m भाग, और IsSigned के साथ IsEncrypted उसकी रिपोर्ट करता है।

// an attachment that is itself a message
SharedPtr<MailMessage> eml = MailMessage::Load(u"WithEmbeddedMsg.msg");
for (SharedPtr<Attachment> attachment : eml->get_Attachments())
{
    Console::WriteLine(attachment->get_Name() + u": " +
        (attachment->get_IsEmbeddedMessage() ? u"an embedded message" : u"a regular attachment"));

    if (attachment->get_IsEmbeddedMessage())
    {
        // the attachment content is a complete message and can be parsed as one
        SharedPtr<MailMessage> inner = MailMessage::Load(attachment->get_ContentStream());
        Console::WriteLine(u"   subject of the embedded message: " + inner->get_Subject());
    }
}

// a signed message carries a single smime.p7m part
SharedPtr<MailMessage> signedEml = MailMessage::Load(u"signed.eml");
Console::WriteLine(u"IsSigned: " + Convert::ToString(signedEml->get_IsSigned()) +
    u", IsEncrypted: " + Convert::ToString(signedEml->get_IsEncrypted()));

for (SharedPtr<Attachment> attachment : signedEml->get_Attachments())
{
    Console::WriteLine(u"Signed email attachment: " + attachment->get_Name());
}

इनलाइन छवियां

एक इनलाइन छवि को अटैचमेंट के रूप में सूचीबद्ध नहीं किया जाता; यह है LinkedResource जिसका ContentId जिसे HTML बॉडी एक cid: URL.

बॉडी में इनलाइन छवि जोड़ें

एक छवि बॉडी में तब जोड़ी जाती है जब इसे जोड़ते हैं LinkedResources एक Content-ID के साथ और HTML से उस ID को संदर्भित करके - पूरा नमूना यहाँ है ईमेल में ऑब्जेक्ट एम्बेड करना. एम्बेडेड ऑब्जेक्ट्स की संख्या केवल उस संदेश आकार से सीमित है जिसे मेल सर्वर स्वीकार करता है।

बॉडी से इनलाइन छवि हटाएँ

RemoveAt और Clear दोनों एक फ़्लैग लेते हैं जो साथ ही हटा देता है <img src=cid:...> HTML से संदर्भ, इसलिए कोई टूटी हुई छवि नहीं बचती। याद रखें कि वैकल्पिक दृश्य अपने स्वयं के संसाधनों की प्रतियां रखते हैं।

SharedPtr<MailMessage> eml = MailMessage::Load(u"EmailWithAttachEmbedded.eml");

Console::WriteLine(u"Linked resources before: " + Convert::ToString(eml->get_LinkedResources()->get_Count()));

// true also removes the <img src=cid:...> reference from the HTML body
eml->get_LinkedResources()->RemoveAt(0, true);

// or drop every inline resource at once
eml->get_LinkedResources()->Clear(true);

// the alternate views hold their own copies of the resources
for (SharedPtr<AlternateView> view : eml->get_AlternateViews())
{
    view->get_LinkedResources()->Clear(true);
}

Console::WriteLine(u"Linked resources after: " + Convert::ToString(eml->get_LinkedResources()->get_Count()));
eml->Save(u"WithoutInlineImages.eml", SaveOptions::get_DefaultEml());

HTML बॉडी की लिंक्ड छवियों को निकालें

कुछ न्यूज़लेटर अपनी छवियों को बिल्कुल भी एम्बेड नहीं करते - HTML बॉडी उन्हें साधारण http लिंक, और संदेश टूटता दिखता है जब ये लिंक काम करना बंद कर देते हैं। सेटिंग ExtractHTMLBodyResourcesAsAttachments लाइब्रेरी को ऐसे संसाधन लाने के लिए कहता है जबकि संदेश निर्यात हो रहा है और उन्हें आउटपुट में एम्बेड करता है। यह ResourceHtmlRendering इवेंट प्रत्येक संसाधन की रिपोर्ट करता है जो प्रोसेस हुआ है।

SharedPtr<MailMessage> mailMessage = MailMessage::Load(u"LinksSample.eml");

SharedPtr<HtmlSaveOptions> options = MakeObject<HtmlSaveOptions>();

// download the images the HTML body links to and store them as attachments
options->set_ExtractHTMLBodyResourcesAsAttachments(true);

// the event reports every resource that is written
options->ResourceHtmlRendering.connect(std::function<void(SharedPtr<Object>, SharedPtr<ResourceHtmlRenderingEventArgs>)>(
    [](SharedPtr<Object> sender, SharedPtr<ResourceHtmlRenderingEventArgs> e)
    {
        Console::WriteLine(u"resource: " + e->get_PathToResourceFile());
    }));

mailMessage->Save(u"WithExtractedResources.html", options);