Aspose.Email for C++ का उपयोग करके ईमेल संदेश बनाना और अनुकूलित करना

Aspose.Email for C++ प्रदान करता है MailMessage क्लास, जो विभिन्न फ़ॉर्मेट में ईमेल संदेश बनाने, अनुकूलित करने और सहेजने के लिए उपयोग होती है। यह क्लास आपको प्रेषक, प्राप्तकर्ता, विषय और बॉडी जैसी आवश्यक प्रॉपर्टीज़ निर्धारित करने की अनुमति देती है और EML, MSG, तथा MHTML फ़ॉर्मेट में संदेश सहेजने का समर्थन करती है।

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

#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <AlternateView.h>
#include <AlternateViewCollection.h>
#include <LinkedResourceCollection.h>
#include <HtmlLoadOptions.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <MsgSaveOptions.h>
#include <MhtSaveOptions.h>
#include <MailPriority.h>
#include <MailSensitivity.h>
#include <DeliveryNotificationOptions.h>
#include <Mime/HeaderCollection.h>
#include <system/text/encoding.h>
#include <system/convert.h>

using namespace System;
using namespace Aspose::Email;

नया ईमेल संदेश बनाना

निम्न कोड नमूना दर्शाता है कि Aspose.Email for C++ का उपयोग करके नया ईमेल संदेश कैसे बनाएं और कॉन्फ़िगर करें।

  1. का एक इंस्टेंस बनाएं MailMessage क्लास।
  2. संदेश प्रॉपर्टीज़ जैसे From, To, Cc, Subject, और HtmlBody सेट करें।
  3. वैकल्पिक रूप से संदेश को कई फ़ॉर्मेट (EML, MSG, MHTML आदि) में सहेजें।

सहेजी गई फ़ाइल का फॉर्मेट द्वारा तय किया जाता है SaveOptions को पास किया गया सहेजें; स्थिर get_Default... विधि तैयार विकल्प लौटाती है प्रत्येक फ़ॉर्मेट के लिए।

SharedPtr<MailMessage> message = MakeObject<MailMessage>();

message->set_Subject(u"New message created by Aspose.Email for C++");
message->set_HtmlBody(u"<b>This line is in bold.</b> <br/> <br/><font color=blue>This line is in blue color</font>");
message->set_From(MakeObject<MailAddress>(u"from@domain.com", u"Sender Name", false));

message->get_To()->Add(MakeObject<MailAddress>(u"to1@domain.com", u"Recipient 1", false));
message->get_To()->Add(MakeObject<MailAddress>(u"to2@domain.com", u"Recipient 2", false));
message->get_CC()->Add(MakeObject<MailAddress>(u"cc1@domain.com", u"Recipient 3", false));
message->get_CC()->Add(MakeObject<MailAddress>(u"cc2@domain.com", u"Recipient 4", false));

// one message, four file formats
message->Save(u"NewMessage.eml", SaveOptions::get_DefaultEml());
message->Save(u"NewMessage.emlx", SaveOptions::get_DefaultEmlx());
message->Save(u"NewMessage.msg", SaveOptions::get_DefaultMsgUnicode());
message->Save(u"NewMessage.mhtml", SaveOptions::get_DefaultMhtml());

ईमेल पतों के लिए फ्रेंडली नामों का उपयोग

एक फ्रेंडली नाम ईमेल पते को अधिक पठनीय बनाता है। उदाहरण के लिए, John Smith <jsmith@domain.com> के बजाय केवल jsmith@domain.com.

A MailAddress फ्रेंडली नाम को अपने कन्स्ट्रक्टर के दूसरे तर्क के रूप में लेता है। C++ में, उस नाम को पास करें एक String, जैसा कि नमूने में: MailAddress एक कन्स्ट्रक्टर भी है जो लेता है bool अपने दूसरे तर्क के रूप में, और एक साधारण स्ट्रिंग लिटरल में परिवर्तित होता है bool की तुलना में अधिक आसानी से String, इसलिए MailAddress(u"kyle@to.com", u"Kyle Huang") एक बिना नाम का पता बना देगा। नीचे का नमूना जोड़ता है To, Cc और Bcc एक मौजूदा संदेश में दोनों पते और एक फ़्रेंडली नाम वाले प्राप्तकर्ता।

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

message->get_To()->Add(MakeObject<MailAddress>(u"kyle@to.com", String(u"Kyle Huang")));
message->get_CC()->Add(MakeObject<MailAddress>(u"guangzhou@cc.com", String(u"Guangzhou Team")));
message->get_Bcc()->Add(MakeObject<MailAddress>(u"ahaq@bcc.com", String(u"Ammad ulHaq")));

message->Save(u"MessageWithFriendlyNames.eml", SaveOptions::get_DefaultEml());

SharedPtr<MailMessage> saved = MailMessage::Load(u"MessageWithFriendlyNames.eml");
for (SharedPtr<MailAddress> address : saved->get_To())
{
    Console::WriteLine(u"To: " + address->get_DisplayName() + u" <" + address->get_Address() + u">");
}

मेल बॉडी कंटेंट सेट करें

यह MailMessage क्लास आपको ईमेल बॉडी को HTML फॉर्मेट में परिभाषित करने की अनुमति देती है। आप विभिन्न ईमेल क्लाइंट्स के लिए वैकल्पिक दृश्य भी प्रदान कर सकते हैं, इसका उपयोग करके AlternateView क्लास।

HTML बॉडी सेट करना

एक स्ट्रिंग को असाइन करना set_HtmlBody संदेश को HTML संदेश बनाता है: मेल क्लाइंट मार्कअप को टेक्स्ट के रूप में दिखाने की बजाय रेंडर करते हैं, और IsBodyHtml रिपोर्ट करता है true.

SharedPtr<MailMessage> message = MakeObject<MailMessage>();
message->set_HtmlBody(u"<html><body>This is the <b>HTML</b> body</body></html>");

Console::WriteLine(u"Is the body HTML? " + Convert::ToString(message->get_IsBodyHtml()));

वैकल्पिक टेक्स्ट सेट करना

कुछ ईमेल क्लाइंट्स HTML सामग्री प्रदर्शित नहीं कर पाते। यह सुनिश्चित करने के लिए कि आपका संदेश सभी प्राप्तकर्ताओं के लिए पढ़ने योग्य हो, आप वैकल्पिक प्लेन टेक्स्ट संस्करण जोड़ सकते हैं, इसका उपयोग करके AlternateView क्लास। यह आपको विभिन्न फॉर्मैट में ईमेल संदेश के कई संस्करण शामिल करने की अनुमति देता है। उदाहरण के लिए, यदि आपका मुख्य संदेश बॉडी HTML है, तो आप वैकल्पिक के रूप में एक प्लेन टेक्स्ट संस्करण भी संलग्न कर सकते हैं।

AlternateView::CreateAlternateViewFromString एक स्ट्रिंग से ऐसा दृश्य बनाता है, और AlternateViews संदेश की संग्रह इसे लेता है।

SharedPtr<MailMessage> message = MakeObject<MailMessage>();
message->set_HtmlBody(u"<p>The <b>formatted</b> version of the message.</p>");

SharedPtr<AlternateView> alternate = AlternateView::CreateAlternateViewFromString(u"The plain text version of the message.");
message->get_AlternateViews()->Add(alternate);

Console::WriteLine(u"Alternate views: " + Convert::ToString(message->get_AlternateViews()->get_Count()));

यह AlternateView क्लास ईमेल बॉडी के भीतर उपयोग किए जाने वाले रिसोर्सेज़ को भी प्रबंधित करती है:

  • LinkedResources – एम्बेडेड आइटम्स (जैसे इमेजेज या अटैचमेंट्स) का संग्रह, जिन्हें संदेश सामग्री में लिंक द्वारा संदर्भित किया जाता है।
  • set_BaseUri() – संदेश बॉडी में रिलेटिव लिंक या रिसोर्स पाथ को हल करने के लिए प्रयुक्त बेस यूआरएल को परिभाषित करता है।

डिफ़ॉल्ट टेक्स्ट एन्कोडिंग सेट करें

PreferredTextEncoding एक एन्कोडिंग सेट करता है सबके लिए जो पाठक देखता है: प्रेषक और प्राप्तकर्ताओं के डिस्प्ले नाम, विषय और बॉडी। इसे पाठ लिखे जाने से पहले असाइन करें, अन्यथा पहले सेट किए गए भाग अपनी पिछली एन्कोडिंग रखेंगे।

SharedPtr<MailMessage> msg = MakeObject<MailMessage>();

// The encoding applied to the display names, the subject and the body
msg->set_PreferredTextEncoding(System::Text::Encoding::GetEncoding(28591));

msg->set_From(MakeObject<MailAddress>(u"dmo@domain.com", String(u"démo")));
msg->get_To()->Add(MakeObject<MailAddress>(u"dmo@domain.com", String(u"démo")));
msg->set_Subject(u"démo");
msg->set_HtmlBody(u"démo");

msg->Save(u"PreferredEncoding.msg", SaveOptions::get_DefaultMsg());

व्यक्तिगत भागों को भी अलग-अलग एन्कोड किया जा सकता है: SubjectEncoding केवल विषय पर लागू होता है और BodyEncoding केवल बॉडी पर लागू।

प्लेन टेक्स्ट बॉडी सेट करें

बॉडी धारण करता है text/plain संदेश का भाग, HtmlBody यह text/html एक। दोनों को सेट करने से दो विकल्पों वाला संदेश बनता है, जो अधिकांश मेल क्लाइंट्स की अपेक्षा होती है।

// plain text body
SharedPtr<MailMessage> plain = MakeObject<MailMessage>();
plain->set_Body(u"This is a plain text body");

// HTML body
SharedPtr<MailMessage> html = MakeObject<MailMessage>();
html->set_HtmlBody(u"<html><body>This is the HTML body</body></html>");

// the encoding used for the body text
html->set_BodyEncoding(System::Text::Encoding::get_UTF8());

Console::WriteLine(u"Plain: " + plain->get_Body());
Console::WriteLine(u"Html: " + html->get_HtmlBody());

IsBodyHtml दोनों प्रकार के संदेशों को अलग करता है: यह है false एक संदेश के लिए जिसमें केवल प्लेन टेक्स्ट बॉडी हो, और true जैसे ही HTML बॉडी मौजूद हो - जब प्लेन टेक्स्ट विकल्प उसके बगल में सेट हो।

SharedPtr<MailMessage> plain = MakeObject<MailMessage>();
plain->set_Body(u"This is a plain text body");

SharedPtr<MailMessage> html = MakeObject<MailMessage>();
html->set_HtmlBody(u"<html><body>This is the HTML body</body></html>");

SharedPtr<MailMessage> both = MakeObject<MailMessage>();
both->set_Body(u"Plain alternative");
both->set_HtmlBody(u"<html><body>HTML alternative</body></html>");

// IsBodyHtml tells whether the message carries an HTML body
Console::WriteLine(u"Plain message, IsBodyHtml: " + Convert::ToString(plain->get_IsBodyHtml()));
Console::WriteLine(u"HTML message, IsBodyHtml: " + Convert::ToString(html->get_IsBodyHtml()));
Console::WriteLine(u"Both bodies, IsBodyHtml: " + Convert::ToString(both->get_IsBodyHtml()));

HTML फ़ाइल से संदेश बनाएं

एक तैयार-निर्मित HTML पृष्ठ संदेश के बॉडी बन सकता है। फ़ाइल को लोड करें HtmlLoadOptions और संकेत PathToResources फ़ोल्डर में जहाँ पृष्ठ द्वारा संदर्भित छवियां होती हैं: उन्हें डिस्क से पढ़ा जाता है और संदेश में इनलाइन संसाधनों के रूप में एम्बेड किया जाता है, इसलिए स्रोत फ़ाइलें हट जाने के बाद भी परिणाम स्वयं-contained रहता है। ShouldAddPlainTextView मार्कअप से एक प्लेन टेक्स्ट विकल्प भी बनाता है।

SharedPtr<HtmlLoadOptions> htmlLoadOptions = MakeObject<HtmlLoadOptions>();
htmlLoadOptions->set_PathToResources(u".");      // folder that holds the referenced images
htmlLoadOptions->set_ShouldAddPlainTextView(true); // add a text/plain alternative

SharedPtr<MailMessage> message = MailMessage::Load(u"body_content.html", htmlLoadOptions);

message->set_From(MakeObject<MailAddress>(u"sender@domain.com"));
message->get_To()->Add(u"receiver@domain.com");
message->set_Subject(u"New message from HTML");

message->Save(u"FromHtml.eml", SaveOptions::get_DefaultEml());

// the images are now embedded in the message, not referenced from disk
Console::WriteLine(u"Linked resources: " + Convert::ToString(message->get_LinkedResources()->get_Count()));
Console::WriteLine(u"Alternate views: " + Convert::ToString(message->get_AlternateViews()->get_Count()));

कई प्राप्तकर्ताओं को निर्दिष्ट करें

प्रति, CC और Bcc हैं MailAddressCollection ऑब्जेक्ट। पते में CC सभी को दिखते हैं; जो हैं उनमें Bcc अन्य प्राप्तकर्ताओं को नहीं दिखाए जाते। Add एक स्ट्रिंग लेता है जो कॉमा-सेपरेटेड सूची को पार्स करता है, इसलिए कई पते एक साथ जोड़े जा सकते हैं।

SharedPtr<MailMessage> eml = MakeObject<MailMessage>();
eml->set_From(u"sender@sender.com");

// primary recipients
eml->get_To()->Add(u"receiver1@receiver.com");
eml->get_To()->Add(u"receiver2@receiver.com");

// carbon copies - visible to everyone
eml->get_CC()->Add(u"cc1@receiver.com");

// blind carbon copies - hidden from the other recipients
eml->get_Bcc()->Add(u"bcc1@receiver.com");

// a comma-separated list is parsed into separate addresses
eml->get_To()->Add(u"receiver3@receiver.com, receiver4@receiver.com");

Console::WriteLine(u"To: " + Convert::ToString(eml->get_To()->get_Count()) +
    u", CC: " + Convert::ToString(eml->get_CC()->get_Count()) +
    u", Bcc: " + Convert::ToString(eml->get_Bcc()->get_Count()));

अतिरिक्त प्रॉपर्टीज़ सेट करें

पते और बॉडी के अलावा, MailMessage संदेश हेडर के अन्य फ़ील्डों को उजागर करता है:

| गुणधर्म | हेडर | | — | — | | Date | Date - निर्माण समय। डिलिवरी समय SMTP सर्वर द्वारा अपने स्वयं के Received हेडर। | | प्राथमिकता | संदेश का महत्व: MailPriority Low, Normal या High. | | संवेदनशीलता | हैंडलिंग संकेत: MailSensitivity Normal, Personal, Private, CompanyConfidential. | | XMailer | X-Mailer - वह प्रोग्राम जिसने संदेश उत्पन्न किया। | | MessageId | Message-ID. | | ReplyToList | Reply-To - जहाँ उत्तर जाने चाहिए। |

SharedPtr<MailMessage> eml = MakeObject<MailMessage>(u"sender@domain.com", u"receiver@domain.com",
    u"Some subject", u"Some body text");

eml->set_Date(DateTime::get_Now());                          // the Date header
eml->set_Priority(MailPriority::High);                       // X-Priority / Importance
eml->set_Sensitivity(MailSensitivity::Normal);               // Sensitivity
eml->set_XMailer(u"Aspose.Email for C++");                   // X-Mailer
eml->set_MessageId(u"<unique-id@domain.com>");               // Message-ID
eml->get_ReplyToList()->Add(u"reply@domain.com");            // Reply-To

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

रीड रसीद का अनुरोध करें

दो अलग-अलग सूचनाएँ अनुरोध की जा सकती हैं। DeliveryNotificationOptions मार्ग में सर्वरों को डिलिवरी स्थिति सूचना के लिए अनुरोध करता है, जबकि ReadReceiptTo प्राप्तकर्ता के मेल क्लाइंट को अनुरोध करता है कि संदेश खोलने की पुष्टि करे।

SharedPtr<MailMessage> eml = MakeObject<MailMessage>();
eml->set_From(u"sender@sender.com");
eml->set_To(u"receiver@receiver.com");
eml->set_HtmlBody(u"<html><body>This is the HTML body</body></html>");

// ask the receiving server for a delivery status notification
eml->set_DeliveryNotificationOptions(DeliveryNotificationOptions::OnSuccess);

// ask the mail client for a read receipt
eml->set_ReadReceiptTo(u"sender@sender.com");

// some clients look at these headers instead
eml->get_Headers()->Add(u"Return-Receipt-To", u"sender@sender.com");

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

रीड रसीद एक अनुरोध है, गारंटी नहीं: क्लाइंट इसे समर्थन नहीं कर सकता, सुविधा बंद हो सकती है, या पाठक बस अस्वीकार कर सकता है। वापस आने वाली सूचना को संभालना यहाँ वर्णित है ईमेल संदेश उन्नत प्रबंधन.

ईमेल हेडर को अनुकूलित करें

वे हेडर फ़ील्ड जिनका समर्पित गुणधर्म नहीं है, उनके द्वारा पहुँचा जाता है Headers, एक HeaderCollection:

  • Add संग्रह के अंत में एक हेडर जोड़ता है;
  • Insert इसे समान नाम वाले मौजूदा हेडर से पहले रखता है, जैसा कि ट्रेस हेडर जैसे Received ज़रूरत;
  • सेट करें हेडर के प्रत्येक मौजूदा मान को प्रतिस्थापित करता है;
  • Get और GetDecodedValue एक मान को वापस पढ़ें।
SharedPtr<MailMessage> eml = MakeObject<MailMessage>();
eml->set_ReplyToList(u"reply@reply.com");
eml->set_From(u"sender@sender.com");
eml->set_To(u"receiver1@receiver.com");
eml->get_CC()->Add(u"receiver2@receiver.com");
eml->get_Bcc()->Add(u"receiver3@receiver.com");
eml->set_Subject(u"test mail");
eml->set_Date(DateTime(2006, 3, 6));
eml->set_XMailer(u"Aspose.Email");

// a custom header, appended at the end of the collection
eml->get_Headers()->Add(u"secret-header", u"mystery");

// Insert places the header before the other headers with the same name
eml->get_Headers()->Insert(u"Received", u"from mail.domain.com");

// read a header back, decoded if it was encoded
Console::WriteLine(eml->get_Headers()->Get(u"secret-header"));
Console::WriteLine(eml->GetHeadersAsString());

GetHeadersAsString पूरे हेडर ब्लॉक को स्ट्रिंग के रूप में लौटाता है जैसा कि फ़ाइल में लिखा जाएगा, जो डिबगिंग के दौरान उपयोगी है।