อ่านและแสดงข้อความอีเมลและส่วนหัวใน C++

Aspose.Email for C++ ให้บริการ MailMessage คลาสที่เป็นตัวแทนของข้อความอีเมลและอนุญาตนักพัฒนาให้เข้าถึงและแสดงเนื้อหาและส่วนหัวของมัน คุณสามารถแยกข้อมูลเช่น ผู้ส่ง, ผู้รับ, หัวเรื่อง, เนื้อหา, และส่วนหัวจากไฟล์อีเมลที่มีอยู่ (EML) ได้อย่างง่ายดาย.

ตัวอย่างในบทความนี้ใช้ส่วนหัวและเนมสเปซต่อไปนี้:

#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <EmlLoadOptions.h>
#include <HeaderType.h>
#include <Mime/HeaderCollection.h>
#include <system/convert.h>
#include <system/text/encoding.h>

using namespace System;
using namespace Aspose::Email;

แสดงข้อมูลอีเมล

โหลดไฟล์อีเมลและแสดงคุณสมบัติหลักบนหน้าจอ เช่น ผู้ส่ง, ผู้รับ, หัวเรื่อง, และเนื้อหา.

  1. โหลดข้อความอีเมลลงใน MailMessage กับ MailMessage::Load.
  2. อ่านคุณสมบัติที่ต้องการ - ตัวอย่างเช่น From, ถึง, Subject, HtmlBody และ Body.

ผู้รับเป็น MailAddressCollection; ของมัน ToString ให้รายการทั้งหมดในรูปแบบที่ใช้ในหัวเรื่องของข้อความ.

SharedPtr<MailMessage> message = MailMessage::Load(u"Message.eml", MakeObject<EmlLoadOptions>());

Console::WriteLine(u"From: " + message->get_From()->ToString());
Console::WriteLine(u"To: " + message->get_To()->ToString());
Console::WriteLine(u"Subject: " + message->get_Subject());
Console::WriteLine(u"HtmlBody: " + message->get_HtmlBody());
Console::WriteLine(u"TextBody: " + message->get_Body());

ดึงส่วนหัวอีเมล

ส่วนหัวอีเมล คือชุดของฟิลด์เมตาดาต้าที่อธิบายข้อความ รวมถึงข้อมูลการส่งต่อ, ชนิดของเนื้อหา, การเข้ารหัส, และรายละเอียดผู้ส่ง/ผู้รับ.

Aspose.Email สำหรับ C++ อนุญาตให้คุณแยกและทำงานกับส่วนหัวเหล่านี้โดยใช้ MailMessage คลาส.

ประเภทส่วนหัวที่พบบ่อยที่สุดสามารถเข้าถึงได้ผ่าน HeaderType คลาสที่ให้ค่าคงที่ที่มีชื่อสำหรับฟิลด์ส่วนหัวมาตรฐาน.

เพื่ออ่านหัวเรื่อง:

  1. โหลดไฟล์อีเมลเข้าสู่ MailMessage.
  2. ดึงคอลเล็กชันส่วนหัวโดยใช้ get_Headers() เมธอด.
  3. วนลูปผ่านคอลเลกชัน: การ enumerating ให้ชื่อหัวเรื่อง, และ Get คืนค่าที่ตำแหน่งหรือค่าของหัวเรื่องที่ตั้งชื่อ.
SharedPtr<MailMessage> message = MailMessage::Load(u"email-headers.eml", MakeObject<EmlLoadOptions>());
SharedPtr<Mime::HeaderCollection> headers = message->get_Headers();

int32_t index = 0;
for (String name : headers)
{
    Console::WriteLine(name + u" - " + headers->Get(index++));
}

// a single header by its name; HeaderType names the standard ones
Console::WriteLine(u"Subject header: " + headers->Get(HeaderType::get_Subject()->ToString()));

รับค่าหัวข้อที่ถอดรหัสแล้ว

ค่าฮีดเดอร์ที่มีข้อความนอก ASCII ถูกเก็บเป็น encoded words (RFC 2047) เช่น =?utf-8?B?...?=. Get คืนค่าโดยที่เก็บไว้, ในขณะที่ GetDecodedValue() ของ HeaderCollection คลาสคืนค่าข้อความที่ผู้อ่านคาดหวัง สำหรับค่าที่ไม่มีคำที่เข้ารหัส ทั้งสองคืนค่าเดียวกัน.

SharedPtr<MailMessage> mailMessage = MailMessage::Load(u"emlWithHeaders.eml");
Console::WriteLine(u"Thread-Topic: " + mailMessage->get_Headers()->GetDecodedValue(u"Thread-Topic"));

// a header with an encoded word
String encoded = u"=?utf-8?B?" + Convert::ToBase64String(Text::Encoding::get_UTF8()->GetBytes(u"Grüße aus Köln")) + u"?=";
mailMessage->get_Headers()->Add(u"X-Greeting", encoded);

Console::WriteLine(u"Stored value:  " + mailMessage->get_Headers()->Get(u"X-Greeting"));
Console::WriteLine(u"Decoded value: " + mailMessage->get_Headers()->GetDecodedValue(u"X-Greeting"));

Extract the Message Body

สองคุณสมบัติเสนอข้อความของข้อความ และพวกมันทำงานแตกต่างกัน:

  • Body คืนค่า text/plain ส่วนเมื่อข้อความมีส่วนหนึ่ง. หากไม่มี, ข้อความธรรมดาจะได้มาจาก HTML body โดยตัด markup ออก.
  • GetHtmlBodyText ทำงานตลอดบน HtmlBody และแปลงเป็นข้อความธรรมดา. Its bool อาร์กิวเมนต์ตัดสินใจว่าเป้าหมายของลิงก์จะถูกเก็บในผลลัพธ์หรือไม่.
SharedPtr<MailMessage> mail = MailMessage::Load(u"HtmlWithUrlSample.eml");

// text/plain part if the message has one, otherwise the HTML body stripped of markup
String plainTextBody = mail->get_Body();
Console::WriteLine(plainTextBody);

// the HTML body as plain text, with and without the link targets
String bodyWithUrl = mail->GetHtmlBodyText(true);
String bodyWithoutUrl = mail->GetHtmlBodyText(false);

Console::WriteLine(u"Body with URL: " + bodyWithUrl);
Console::WriteLine(u"Body without URL: " + bodyWithoutUrl);

เมธอดเดียวกันยังรับคอลแบ็กที่ตัดสินใจว่าทำอย่างไรกับแต่ละไฮเปอร์ลิงก์; ดู การจัดการขั้นสูงของข้อความอีเมล.

อ่านที่อยู่ Exchange X.500

ข้อความที่เดินทางผ่าน Exchange มักบรรจุผู้ส่งและผู้รับในรูปแบบไดเรกทอรี X.500 (/o=.../ou=.../cn=...) แทนหรือเพิ่มเติมกับที่อยู่อีเมล SMTP. MailAddress เผยโดย X500Address; property เป็นสตริงว่างเมื่อข้อความไม่มีที่อยู่ดังกล่าว.

SharedPtr<MailMessage> mailMessage = MailMessage::Load(u"Contact.msg");

// the Exchange address of the sender, if the message carries one
String exchangeAddress = mailMessage->get_From()->get_X500Address();

if (!String::IsNullOrEmpty(exchangeAddress))
{
    Console::WriteLine(u"X.500 address: " + exchangeAddress);
}
else
{
    Console::WriteLine(u"The message has no X.500 address; SMTP address: " + mailMessage->get_From()->get_Address());
}