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;
이메일 정보 표시
이메일 파일을 로드하고 화면에 발신자, 수신자, 제목, 본문 등 주요 속성을 표시합니다.
- 이메일 메시지를 a에 로드합니다 MailMessage 와 함께 MailMessage::Load.
- 원하는 속성을 읽습니다 - 예를 들어 From, To, Subject, HtmlBody 및 본문.
받는 사람은 a 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());
이메일 헤더 추출
email header는 라우팅 정보, 콘텐츠 유형, 인코딩 및 발신자/수신자 세부 정보를 포함한 메시지를 설명하는 메타데이터 필드 집합입니다.
Aspose.Email for C++를 사용하면 다음을 통해 이러한 헤더를 추출하고 작업할 수 있습니다: MailMessage 클래스.
가장 일반적인 헤더 유형은 다음을 통해 사용할 수 있습니다: HeaderType 클래스는 표준 헤더 필드에 대한 명명된 상수를 제공합니다.
헤더를 읽으려면:
- 이메일 파일을 MailMessage.
- 다음 을 사용하여 헤더 컬렉션을 검색합니다: get_Headers() 메서드.
- 컬렉션을 반복합니다: 열거하면 헤더 이름이 반환되고, 그리고 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 텍스트를 포함하는 헤더값은 인코딩된 단어 (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"));
메시지 본문 추출
두 개의 속성이 메시지 텍스트를 노출하며, 동작 방식이 다릅니다:
- 본문 반환합니다
text/plain부분에 대해 메시지에 하나가 있으면 작동합니다. 없을 경우 HTML 본문에서 마크업을 제거하여 평문을 도출합니다. - GetHtmlBodyText 항상 HtmlBody 그리고 이를 평문 텍스트로 변환합니다. 그
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);
같은 메서드는 각 하이퍼링크가 어떻게 렌더링될지를 결정하는 콜백도 받으며; 자세히 보려면 이메일 메시지 고급 관리.
X.500 Exchange 주소 읽기
Exchange를 거친 메시지는 발신자와 수신자를 X.500 디렉터리 형식으로 포함하는 경우가 많습니다 (/o=.../ou=.../cn=...) 대신 혹은 SMTP 주소와 추가로. MailAddress 이를 통해 노출합니다 X500Address; 메시지가 해당 주소를 포함하지 않으면 속성은 빈 문자열입니다.
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());
}