ユーティリティ機能 - MailMessage
TNEF 添付ファイルを含む MailMessage
Transport Neutral Encapsulation Format(TNEF)は、Microsoft Outlook および Microsoft Exchange Server が使用する独自のメール添付形式です。Aspose.Email API を使用すると、TNEF 添付ファイルを含むメールメッセージを読み取り、添付ファイルの内容を変更できます。その後、メールは通常のメールとして、または同じ形式で保存でき、TNEF 添付ファイルを保持します。本稿では、TNEF 添付ファイルを含むメッセージを扱うさまざまなコード例を示します。また、Outlook MSG ファイルから TNEF EML ファイルを作成する方法も示します。
TNEF 添付ファイルを保持してメッセージを読み取る
次のコードスニペットは、TNEF 添付ファイルを保持したままメッセージを読み取る方法を示しています。
from aspose.email import MailMessage, SaveOptions, EmlLoadOptions, MessageFormat, FileCompatibilityMode
options = EmlLoadOptions()
# This will Preserve the TNEF attachment as it is, file contains the TNEF attachment
options.preserve_tnef_attachments = True
eml = MailMessage.load(data_dir + "message.eml", options)
for attachment in eml.attachments:
print(attachment.name)
MSG から TNEF EML を作成
Outlook の MSG ファイルには、テーブルやテキストスタイルなどの情報が含まれていることがあり、これらを EML に変換すると乱れる可能性があります。そのような MSG ファイルから TNEF メッセージを作成すると、書式を保持したまま、メールクライアントを通じて送信することも可能です。convert_as_tnef プロパティを使用してこれを実現します。次のコードスニペットは、MSG から TNEF EML を作成する方法を示しています。
from aspose.email.mapi import MapiMessage, MailConversionOptions, OutlookMessageFormat
mapi_msg = MapiMessage.from_file(data_dir + "message.msg")
mail_conversion_options = MailConversionOptions()
mail_conversion_options.convert_as_tnef = True
message = mapi_msg.to_mail_message(mail_conversion_options)
TNEF を作成するには、以下のサンプルコードを使用できます。
from aspose.email import MailMessage, SaveOptions, MsgLoadOptions, MessageFormat, FileCompatibilityMode
options = MsgLoadOptions()
# The PreserveTnefAttachments option with MessageFormat.Msg will create the TNEF eml.
options.preserve_tnef_attachments = True
eml = MailMessage.load(eml_file_name, options)
メッセージが TNEF かどうかの検出
次のコードスニペットは、メッセージが TNEF かどうかを検出する方法を示しています。
from aspose.email import MailMessage
mail = MailMessage.load(data_dir + "message.eml")
is_tnef = mail.original_is_tnef
バウンスメッセージの処理
受信者に送信されたメッセージが、無効な受信者アドレスなどの理由でバウンスすることは非常に一般的です。Aspose.Email API は、そのようなメッセージがバウンスメールか通常のメールかをチェックする機能を備えています。MailMessage の check_bounced メソッドは、メールがバウンスメールである場合に有効な結果を返します。本稿では、 BounceResult メッセージがバウンスメールかどうかをチェックする機能を提供するクラスです。さらに、受信者、実行されたアクション、通知の理由に関する詳細情報を提供します。次のコードスニペットは、バウンスメッセージを処理する方法を示しています。
from aspose.email import MailMessage, SaveOptions, MsgLoadOptions, MessageFormat, FileCompatibilityMode
mail = MailMessage.load(data_dir + "message.eml")
result = mail.check_bounced()
print("IsBounced: " + str(result.is_bounced))
print("Action: " + str(result.action))
print("Recipient: " + str(result.recipient))
print()
print("Reason: " + str(result.reason))
print("Status: " + str(result.status))
print()
ベイジアン スパムアナライザー
Aspose.Email はベイジアン・スパム分析器を使用したメールフィルタリングを提供します。 SpamAnalyzer この目的のためのクラスです。本稿では、単語データベースに基づいてスパムと通常のメールを区別するフィルタの学習方法を示します。
from aspose.email import MailMessage, SaveOptions, MsgLoadOptions, MessageFormat, FileCompatibilityMode
from aspose.email.antispam import SpamAnalyzer
import os
ham_folder = "/hamFolder"
spam_folder = "/Spam"
test_folder = data_dir
database_file = "SpamFilterDatabase.txt"
def print_result(probability):
if probability >= 0.5:
print("The message is classified as spam.")
else:
print("The message is classified as not spam.")
print("Spam Probability: " + str(probability))
print()
def teach_and_create_database(ham_folder, spam_folder, database_file):
analyzer = SpamAnalyzer(database_file)
analyzer.teach_from_directory(ham_folder, True)
analyzer.teach_from_directory(spam_folder, False)
analyzer.save_database()
teach_and_create_database(ham_folder, spam_folder, database_file)
test_files = [f for f in os.listdir(test_folder) if f.endswith(".eml")]
analyzer = SpamAnalyzer(database_file)
for file in test_files:
file_path = os.path.join(test_folder, file)
msg = MailMessage.load(file_path)
print(msg.subject)
probability = analyzer.test(msg)
print_result(probability)