C++ में ईमेल कैसे भेजें?

इस ट्यूटोरियल में, हम यह पता लगाएंगे कि C++** में **Email कैसे भेजें। आप सरल एपीआई कॉल के साथ सी++ में जीमेल सर्वर पर एसएमटीपी क्लाइंट का उपयोग करके मेल भेजना सीखेंगे। विंडोज और लिनक्स प्लेटफॉर्म में एपीआई का उपयोग करने के लिए आपको माइक्रोसॉफ्ट आउटलुक या किसी अन्य ईमेल क्लाइंट को स्थापित करने की आवश्यकता नहीं है।

सी++ में ईमेल भेजने के चरण

  1. NuGet पैकेज मैनेजर से Aspose.Email for C++ इंस्टॉल करें
  2. Aspose::Email नाम स्थान का संदर्भ जोड़ें
  3. SmtpClient Class उदाहरण में क्रेडेंशियल का उपयोग करके Gmail मेलबॉक्स से कनेक्ट करें
  4. MailMessage Class उदाहरण का उपयोग करके डिस्क से ईमेल संदेश लोड करें
  5. एसएमटीपी क्लाइंट द्वारा उजागर भेजें का उपयोग करके सी ++ में ईमेल संदेश भेजें

आप कोड की कुछ पंक्तियों में सी++* में जीमेल से जुड़े एसएमटीपी क्लाइंट का उपयोग करके आसानी से *ईमेल भेज सकते हैं। इस उदाहरण में, हमने दिखाया है कि आप SMTP का उपयोग करके EML प्रारूप ईमेल कैसे भेज सकते हैं। आप MSG प्रारूप ईमेल संदेश भी भेज सकते हैं और उदाहरण के लाइन 42 पर MsgLoadOption क्लास का उपयोग करना होगा।

सी++ में ईमेल भेजने के लिए कोड

#pragma once
#include <system/object.h>
#include <system/array.h>
#include <system/string.h>
#include <system/shared_ptr.h>
#include <system/object_ext.h>
#include <system/exceptions.h>
#include <system/environment.h>
#include <system/diagnostics/trace.h>
#include <system/console.h>
#include <MailMessage.h>
#include <EmlLoadOptions.h>
#include <Clients/Smtp/SmtpClient/SmtpClient.h>
#pragma once
#include <Clients/SecurityOptions.h>
#include <system/io/path.h>
#include <system/io/directory_info.h>
#include <system/io/directory.h>
#include <Licensing/License.h>
using namespace Aspose::Email::Clients::Smtp;
using namespace Aspose::Email::Clients;
using namespace Aspose::Email;
using namespace System;
void SendingEMLWithSMTP()
{
// Set the license for Aspose.Email for CPP
SharedPtr<License> license = System::MakeObject<License>();
license->SetLicense(u"licFile");
// The path to the source EML file to be send
System::String dstEmail = u"Message.eml";
// Ue MailMessage class to load the email Message from disk
System::SharedPtr<MailMessage> message = System::MakeObject<MailMessage>();
// Load the file in EML format
message = MailMessage::Load(dstEmail, System::MakeObject<EmlLoadOptions>());
// Instantiate SmtpClient class object to connect to SMTP server
System::SharedPtr<SmtpClient> client = System::MakeObject<SmtpClient>(u"smtp.gmail.com", 587,
u"your.email@gmail.com", u"your.password");
client->set_SecurityOptions(Aspose::Email::Clients::SecurityOptions::Auto);
try
{
// Send the message
client->Send(message);
System::Console::WriteLine(u"Message has been sent");
}
catch (System::Exception& ex)
{
System::Diagnostics::Trace::WriteLine(System::ObjectExt::ToString(ex));
}
System::Console::WriteLine(System::Environment::get_NewLine() + u"Email sent using EML file successfully. " + dstEmail);
}

इससे पहले, हमने सी # में ईमेल कैसे भेजें सीखा था। जबकि, यह विषय इस बात पर केंद्रित है कि आप C++* में SMTP क्लाइंट का उपयोग करके Gmail सर्वर पर *ईमेल कैसे भेज सकते हैं। इस उदाहरण का उपयोग करते समय, आपको एक अपवाद मिल सकता है जैसे “SMTP सर्वर को एक सुरक्षित कनेक्शन की आवश्यकता है या क्लाइंट को प्रमाणित नहीं किया गया था”। वास्तव में, जीमेल को आपके एप्लिकेशन को कम सुरक्षित होने का संदेह है और आपको उनके एसएमटीपी सेवर का उपयोग करके ईमेल भेजने से मना करता है। Gmail SMTP समस्या को हल करने के लिए कृपया विषय पर जाएँ, allow access to less secure apps to send email

 हिन्दी