คุณสมบัติเครื่องมือ - MailMessage
ข้อความอีเมลที่มีไฟล์แนบ TNEF
Transport Neutral Encapsulation Format (TNEF) คือรูปแบบไฟล์แนบของอีเมลที่เป็นกรรมสิทธิ์ของ Microsoft Outlook และ Microsoft Exchange Server Aspose.Email API อนุญาตให้คุณอ่านข้อความอีเมลที่มีไฟล์แนบ TNEF และแก้ไขเนื้อหาของไฟล์แนบนั้น จากนั้นอีเมลสามารถบันทึกเป็นอีเมลธรรมดาหรือในรูปแบบเดียวกันโดยคงไฟล์แนบ TNEF ไว้ บทความนี้แสดงตัวอย่างโค้ดหลายชุดสำหรับทำงานกับข้อความที่มีไฟล์แนบ TNEF อีกทั้งยังแสดงวิธีการสร้างไฟล์ TNEF EML จากไฟล์ Outlook MSG
การอ่านข้อความโดยคงไฟล์แนบ 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)
สร้าง TNEF EML จาก MSG
ไฟล์ Outlook MSG บางครั้งมีข้อมูลเช่น ตารางและรูปแบบข้อความที่อาจเสียหายหากแปลงเป็น EML การสร้างข้อความ TNEF จากไฟล์ MSG ดังกล่าวช่วยรักษาการจัดรูปแบบและแม้แต่ส่งข้อความผ่านไคลเอนต์อีเมลโดยคงรูปแบบไว้ คุณสมบัติ convert_as_tnef ถูกใช้เพื่อทำเช่นนี้ โค้ดตัวอย่างต่อไปนี้แสดงวิธีการสร้างไฟล์ TNEF eML จาก MSG.
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
การประมวลผลข้อความที่ Bounce
เป็นเรื่องทั่วไปที่ข้อความที่ส่งถึงผู้รับอาจ bounce ด้วยเหตุผลใด ๆ เช่น ที่อยู่อีเมลผู้รับไม่ถูกต้อง Aspose.Email API มีความสามารถในการประมวลผลข้อความดังกล่าวเพื่อพิจารณาว่าเป็นอีเมลที่ bounce หรือเป็นอีเมลปกติ เมธอด check_bounced ของ MailMessage จะคืนค่าที่ถูกต้องหากข้อความอีเมลเป็น bounce บทความนี้แสดงการใช้ BounceResult คลาสที่ให้ความสามารถในการตรวจสอบว่าข้อความเป็นอีเมลที่ bounced หรือไม่ นอกจากนี้ยังให้ข้อมูลรายละเอียดเกี่ยวกับผู้รับ การดำเนินการที่ทำ และเหตุผลของการแจ้งเตือน โค้ดตัวอย่างต่อไปนี้แสดงวิธีการประมวลผลข้อความที่ bounced.
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)