C++ Requires Chilkat v11.0.0+
C++
Read email headers and display the body as text
See more Email Object Examples
Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.
Chilkat C++ Downloads
#include <CkEmail.h>
#include <CkHtmlToText.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkEmail email;
success = email.LoadEml("with-attachments.eml");
if (success != true) {
std::cout << email.lastErrorText() << "\r\n";
return;
}
std::cout << "From: " << email.ck_from() << "\r\n";
std::cout << "Subject: " << email.subject() << "\r\n";
std::cout << "Date: " << email.emailDateStr() << "\r\n";
int i = 0;
while ((i < email.get_NumTo())) {
std::cout << "To: " << email.getToName(i) << " " << email.getToAddr(i) << "\r\n";
i = i + 1;
}
const char *referenceId = email.getHeaderField("X-Reference-ID");
if (email.get_LastMethodSuccess() == true) {
std::cout << "Reference: " << referenceId << "\r\n";
}
if (email.HasPlainTextBody() == true) {
std::cout << email.getPlainTextBody() << "\r\n";
}
else {
if (email.HasHtmlBody() == true) {
CkHtmlToText reader;
std::cout << reader.toText(email.getHtmlBody()) << "\r\n";
}
else {
std::cout << "No text or HTML body." << "\r\n";
}
}
}