Šioje mokymo programoje išnagrinėsime, kaip Siųsti Email C++. Sužinosite, kaip siųsti laiškus naudojant SMTP klientą Gmail serveryje C++ naudojant paprastus API skambučius. Norint naudoti API Windows ir Linux platformose, nereikia įdiegti Microsoft Outlook ar jokios kitos el. pašto programos.
El. pašto siuntimo veiksmai C++
- Įdiekite Aspose.Email for C++ iš NuGet paketų tvarkytuvės
- Pridėkite nuorodą į Aspose::Email vardų erdvę
- Prisijunkite prie Gmail pašto dėžutės naudodami kredencialus SmtpClient Class egzemplioriuje
- Įkelkite el. pašto pranešimą iš disko naudodami MailMessage Class egzempliorių
- Naudodami Siųsti, atskleistą SMTP kliento, siųskite el. laišką C++
Galite lengvai siųsti el. laišką naudodami SMTP klientą, prijungtą prie Gmail C++, naudodami kelias kodo eilutes. Šiame pavyzdyje parodėme, kaip galite siųsti EML formato el. laišką naudodami SMTP. Taip pat galite siųsti MSG formato el. laišką ir reikės naudoti MsgLoadOption klasę pavyzdžio 42 eilutėje.
Kodas el. pašto siuntimui C++
#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); | |
} |
Anksčiau sužinojome Kaip siųsti el. laišką C#. Tuo tarpu šioje temoje dėmesys sutelkiamas į tai, kaip galite siųsti el. laišką Gmail serveryje naudodami SMTP klientą C++. Naudodami šį pavyzdį galite gauti išimtį, pvz., “SMTP serveriui reikalingas saugus ryšys arba klientas nebuvo autentifikuotas”. Tiesą sakant, Gmail įtaria, kad jūsų programa yra mažiau saugi ir draudžia siųsti el. laiškus naudojant jų SMTP serverį. Norėdami išspręsti Gmail SMTP problemą, apsilankykite temoje allow access to less secure apps to send email.