Dalam tutorial ini, kita akan mengeksplorasi cara Kirim Email di C++. Anda akan belajar cara mengirim email menggunakan klien SMTP di Server Gmail di C++ dengan panggilan API sederhana. Anda tidak perlu menginstal Microsoft Outlook atau klien Email lainnya untuk menggunakan API di platform Windows dan Linux.
Langkah-langkah Mengirim Email di C++
- Instal Aspose.Email for C++ dari Pengelola paket NuGet
- Tambahkan referensi ke Aspose::Email namespace
- Hubungkan ke kotak surat Gmail dengan menggunakan kredensial di SmtpClient Class instance
- Muat Pesan Email dari disk menggunakan instance Kelas MailMessage
- Dengan menggunakan Kirim yang diekspos oleh klien SMTP, kirim Pesan email dalam C++
Anda dapat dengan mudah mengirim email menggunakan SMTP Client yang terhubung ke Gmail di C++ dalam beberapa baris kode. Dalam contoh ini, kami telah menunjukkan bagaimana Anda dapat mengirim email berformat EML menggunakan SMTP. Anda juga dapat mengirim pesan email berformat MSG dan harus menggunakan Kelas MsgLoadOption di baris 42 dari contoh.
Kode untuk Mengirim Email di 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); | |
} |
Sebelumnya, kita telah mempelajari Cara Mengirim Email di C#. Padahal, topik ini berfokus pada bagaimana Anda dapat mengirim Email di Server Gmail menggunakan Klien SMTP di C++. Saat menggunakan contoh ini, Anda mungkin mendapatkan pengecualian seperti “Server SMTP memerlukan koneksi aman atau klien tidak diautentikasi”. Sebenarnya, Gmail mencurigai aplikasi Anda kurang aman dan melarang Anda mengirim email menggunakan server SMTP mereka. Untuk mengatasi masalah SMTP Gmail, kunjungi topik, allow access to less secure apps to send email.