メールメッセージ高度管理
この記事はメッセージの作成・保存を超える機能をまとめています:メッセージのレンダリング制御、スパムフィルタリング、MIME 構造の隠れ部分の読み取り、既読追跡など。
以下のサンプルはこれらのヘッダーと名前空間を使用しています:
#include <MailMessage.h>
#include <MailAddressCollection.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <MhtSaveOptions.h>
#include <MhtFormatOptions.h>
#include <MhtTemplateName.h>
#include <system/collections/list_ext.h>
#include <system/collections/specialized/string_dictionary.h>
#include <Common/Transform/HtmlToTextConverter.h>
#include <AntiSpam/SpamAnalyzer.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiConversionOptions.h>
#include <system/io/directory.h>
#include <system/io/path.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::AntiSpam;
カスタムスタイルでハイパーリンクをレンダリング
GetHtmlBodyText HTML 本文をプレーンテキストに変換します。第2オーバーロードは HyperlinkRenderingCallback - すべてのリンクの完全マークアップを受け取り、置換すべきテキストを返すデリゲートです。ここでリンクアドレスを保持するか、装飾するか、完全に削除するかを決めます。
// Receives the <a ...>text</a> markup of every link and returns its replacement
String RenderHyperlinkWithHref(String source)
{
int32_t start = source.IndexOf(u"href=\"") + 6;
int32_t end = source.IndexOf(u"\"", start);
String href = source.Substring(start, end - start);
start = source.IndexOf(u">") + 1;
end = source.IndexOf(u"<", start);
String text = source.Substring(start, end - start);
return String::Format(u"{0}<{1}>", text, href);
}
// Keeps only the visible text of the link
String RenderHyperlinkWithoutHref(String source)
{
int32_t start = source.IndexOf(u">") + 1;
int32_t end = source.IndexOf(u"<", start);
return source.Substring(start, end - start);
}
SharedPtr<MailMessage> msg = MailMessage::Load(u"LinksSample.eml");
Console::WriteLine(msg->GetHtmlBodyText(Common::Transform::HyperlinkRenderingCallback(&RenderHyperlinkWithHref)));
Console::WriteLine(msg->GetHtmlBodyText(Common::Transform::HyperlinkRenderingCallback(&RenderHyperlinkWithoutHref)));
最初のコールバックは <a href="https://www.aspose.com/">Aspose.Email</a> へ Aspose.Email<https://www.aspose.com/>、2 番目は残します Aspose.Email.
MHTML に表示するヘッダーを選択
メッセージが MHTML にエクスポートされると、ライブラリは本文の上にヘッダー行ブロックを書き込みます。 RenderingHeaders の MhtSaveOptions どの行が表示され、どの順序になるかを決定します;名前は MhtTemplateName クラスです。空のコレクションはデフォルトブロックを意味します。
SharedPtr<MailMessage> eml = MailMessage::Load(u"Attachments.eml");
SharedPtr<MhtSaveOptions> options = SaveOptions::get_DefaultMhtml();
// the default header block
eml->Save(u"MhtDefaultOrder.mhtml", options);
// only these headers, in this order
options->get_RenderingHeaders()->Add(MhtTemplateName::From);
options->get_RenderingHeaders()->Add(MhtTemplateName::Subject);
options->get_RenderingHeaders()->Add(MhtTemplateName::To);
options->get_RenderingHeaders()->Add(MhtTemplateName::Sent);
eml->Save(u"MhtCustomOrder.mhtml", options);
// a different selection
options->get_RenderingHeaders()->Clear();
options->get_RenderingHeaders()->Add(MhtTemplateName::Attachments);
options->get_RenderingHeaders()->Add(MhtTemplateName::Cc);
options->get_RenderingHeaders()->Add(MhtTemplateName::Subject);
eml->Save(u"MhtAttachmentsFirst.mhtml", options);
すべてのヘッダーを書き出す
上のブロックは読者向けの要約です。すべての MIME ヘッダーを MHTML ファイルに入れるには、次に切り替えます SaveAllHeaders です。
SharedPtr<MailMessage> eml = MailMessage::Load(u"email-headers.eml");
SharedPtr<MhtSaveOptions> options = SaveOptions::get_DefaultMhtml();
options->set_SaveAllHeaders(true);
eml->Save(u"AllHeaders.mhtml", options);
カレンダーイベントをレンダリング
MHTML として保存された会議は、 MhtFormatOptions 両方を含みます RenderCalendarEvent および WriteHeader:
SharedPtr<MailMessage> eml = MailMessage::Load(u"Test_Meeting.msg");
SharedPtr<MhtSaveOptions> options = MakeObject<MhtSaveOptions>();
options->set_MhtFormatOptions(MhtFormatOptions::RenderCalendarEvent | MhtFormatOptions::WriteHeader);
eml->Save(u"MeetingWithAttendees.mhtml", options);
ブロックの個々の行は次を通じてフォーマットされます FormatTemplates、同じキーで辞書化されたものです MhtTemplateName 定数; キーに空文字列を設定すると出力からその行が削除されます。
ベイジアン・スパム解析器でメッセージをスコア付け
SpamAnalyzer ベイズフィルタを実装します。まず、すでに分類したメッセージで学習させます。 TrainFilter メッセージとスパムかどうかのフラグを受け取ります。 SaveDatabase 結果の単語統計をファイルに書き込み、後の実行時にコンストラクタまたは LoadDatabase. テスト その後、スパムである確率(0〜1)を返します。
// Train the filter and store the word database
SharedPtr<SpamAnalyzer> teacher = MakeObject<SpamAnalyzer>();
for (String file : IO::Directory::GetFiles(u"hamFolder", u"*.eml"))
{
// false marks the message as a regular one
teacher->TrainFilter(MailMessage::Load(file), false);
}
for (String file : IO::Directory::GetFiles(u"spamFolder", u"*.eml"))
{
// true marks the message as spam
teacher->TrainFilter(MailMessage::Load(file), true);
}
teacher->SaveDatabase(u"SpamFilterDatabase.txt");
// Score new messages with the stored database
SharedPtr<SpamAnalyzer> analyzer = MakeObject<SpamAnalyzer>(u"SpamFilterDatabase.txt");
for (String file : IO::Directory::GetFiles(u"testFolder", u"*.eml"))
{
SharedPtr<MailMessage> msg = MailMessage::Load(file);
// a value between 0 and 1
double probability = analyzer->Test(msg);
Console::WriteLine(IO::Path::GetFileName(file) + u": " +
(probability > 0.5 ? u"spam" : u"not spam") + u" (" + Convert::ToString(probability) + u")");
}
トレーニングコーパスが大きく、代表性が高いほど、スコアは有用になります。少数のメッセージでも範囲の中間に近い値が得られます。
プレアンブルとエピローグを読む
マルチパートメッセージは、最初のパートの前と最後のパートの後にテキストを含めることができます。メールクライアントはこれを表示しません—プレアンブルは伝統的に「This is a multi-part message in MIME format.」という行を保持します—しかし、このフィールドは余分な情報をすり込むために使用されることがあります。 プレアンブル および エピローグ 読み取りと書き込みの両方のアクセス権を付与します。
SharedPtr<MailMessage> eml = MailMessage::Load(u"Attachments.eml");
// text placed before the first MIME part and after the last one
Console::WriteLine(u"Preamble: " + eml->get_Preamble());
Console::WriteLine(u"Epilogue: " + eml->get_Epilogue());
// both properties can be written as well
eml->set_Preamble(u"This is a multi-part message in MIME format.");
eml->set_Epilogue(u"End of the message.");
eml->Save(u"WithPreamble.eml", SaveOptions::get_DefaultEml());
SharedPtr<MailMessage> reloaded = MailMessage::Load(u"WithPreamble.eml");
Console::WriteLine(u"Reloaded preamble: " + reloaded->get_Preamble());
MDN と開封確認によるメール追跡
Message Disposition Notification(MDN)は、送信者にメッセージが開封されたことを通知します。送信者は次を通じて要求します ReadReceiptTo、単一のアドレスを文字列として、または全体として受け入れるものです MailAddressCollection。受信側では CreateReadReceipt 通知メッセージを構築し、SMTP クライアントで返送します。Outlook アイテムの場合、要求は次のように表現されます MapiMessage::ReadReceiptRequested.
// Sender side: ask for a read receipt
SharedPtr<MailMessage> outgoing = MakeObject<MailMessage>(u"from@domain.com", u"to@domain.com",
u"test MDN", u"This is a test message with a read receipt requested");
// a single address can be assigned as a string
outgoing->set_ReadReceiptTo(u"from@domain.com");
// or several addresses can be collected
SharedPtr<MailAddressCollection> addresses = MakeObject<MailAddressCollection>();
addresses->Add(u"from@domain.com");
addresses->Add(u"another@domain.com");
outgoing->set_ReadReceiptTo(addresses);
outgoing->Save(u"MdnRequested.eml", SaveOptions::get_DefaultEml());
// Recipient side: build the notification for a received message
SharedPtr<MailMessage> received = MailMessage::Load(u"MdnRequested.eml");
if (received->get_ReadReceiptTo()->get_Count() > 0)
{
SharedPtr<MailMessage> receipt = received->CreateReadReceipt(u"to@domain.com", nullptr);
receipt->Save(u"ReadReceipt.eml", SaveOptions::get_DefaultEml());
Console::WriteLine(u"Receipt subject: " + receipt->get_Subject());
}
// The same request in an Outlook message
SharedPtr<MapiMessage> mapiMessage = MapiMessage::FromMailMessage(outgoing, MapiConversionOptions::get_UnicodeFormat());
mapiMessage->set_ReadReceiptRequested(true);
mapiMessage->Save(u"MdnRequested.msg");
通知は CreateReadReceipt 件名があります Read: <original subject> そして第2引数として渡された本文テキスト; nullptr デフォルトのものを残します。