วิธีการส่งอีเมลใน C++

ในบทช่วยสอนนี้ เราจะศึกษาวิธีการ ส่ง Email ใน C+* คุณจะได้เรียนรู้วิธีส่งอีเมลโดยใช้ไคลเอนต์ SMTP บน Gmail Server ในภาษา C++ ด้วยการเรียก API อย่างง่าย คุณไม่จำเป็นต้องติดตั้ง Microsoft Outlook หรือไคลเอ็นต์อีเมลอื่นๆ เพื่อใช้ API ในแพลตฟอร์ม Windows และ Linux

ขั้นตอนในการส่งอีเมลใน C++

  1. ติดตั้ง Aspose.Email for C++ จาก NuGet package Manager
  2. เพิ่มการอ้างอิงไปยังเนมสเปซ Aspose::Email
  3. เชื่อมต่อกับกล่องจดหมาย Gmail โดยใช้ข้อมูลรับรองในอินสแตนซ์ SmtpClient Class
  4. โหลดข้อความอีเมลจากดิสก์โดยใช้อินสแตนซ์ MailMessage Class
  5. โดยใช้การส่งเปิดเผยโดยไคลเอนต์ SMTP ส่งอีเมลข้อความใน C ++

คุณสามารถ ส่งอีเมลโดยใช้ไคลเอนต์ SMTP ที่เชื่อมต่อกับ Gmail ใน C++ ได้ง่ายๆ โดยใช้โค้ดไม่กี่บรรทัด ในตัวอย่างนี้ เราได้แสดงวิธีที่คุณสามารถส่งอีเมลรูปแบบ EML โดยใช้ SMTP คุณยังสามารถส่งข้อความอีเมลรูปแบบ MSG ได้เช่นกัน และจะต้องใช้ MsgLoadOption Class ที่ บรรทัดที่ 42 ของตัวอย่าง

รหัสเพื่อส่งอีเมลใน 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);
}

ก่อนหน้านี้ เราได้เรียนรู้เกี่ยวกับ วิธีการส่งอีเมลใน C# ในขณะที่หัวข้อนี้มุ่งเน้นไปที่วิธีที่คุณสามารถ *ส่งอีเมลบนเซิร์ฟเวอร์ Gmail โดยใช้ไคลเอนต์ SMTP ใน C++ ขณะใช้ตัวอย่างนี้ คุณอาจได้รับข้อยกเว้น เช่น เซิร์ฟเวอร์ SMTP ต้องการการเชื่อมต่อที่ปลอดภัย หรือไคลเอ็นต์ไม่ได้รับการตรวจสอบสิทธิ์ ที่จริงแล้ว Gmail สงสัยว่าแอปพลิเคชันของคุณมีความปลอดภัยน้อย และห้ามไม่ให้คุณส่งอีเมลโดยใช้เซิร์ฟเวอร์ SMTP หากต้องการแก้ไขปัญหา SMTP ของ Gmail โปรดไปที่หัวข้อ allow access to less secure apps to send email

 ไทย