Tính Năng Tiện Ích - MailMessage
MailMessages chứa tệp đính kèm TNEF
Transport Neutral Encapsulation Format (TNEF) là định dạng tệp đính kèm email độc quyền được Microsoft Outlook và Microsoft Exchange Server sử dụng. API Aspose.Email cho phép bạn đọc các tin nhắn email có đính kèm TNEF và chỉnh sửa nội dung của tệp đính kèm. Email sau đó có thể được lưu dưới dạng email bình thường hoặc cùng định dạng, bảo lưu các tệp đính kèm TNEF. Bài viết này trình bày các mẫu mã khác nhau để làm việc với các tin nhắn chứa tệp đính kèm TNEF. Bài viết này cũng giới thiệu cách tạo tệp TNEF EML từ các tệp Outlook MSG.
Đọc Tin nhắn bằng cách Bảo lưu Các Tệp Đính Kèm TNEF
Đoạn mã sau đây cho bạn thấy cách đọc tin nhắn bằng cách bảo lưu các tệp đính kèm 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)
Tạo TNEF EML từ MSG
Các tệp Outlook MSG đôi khi chứa thông tin như bảng và kiểu văn bản có thể bị mất khi chuyển đổi sang EML. Tạo tin nhắn TNEF từ các tệp MSG như vậy cho phép giữ nguyên định dạng và thậm chí gửi các tin nhắn này qua client email mà vẫn giữ định dạng. Thuộc tính convert_as_tnef được sử dụng để đạt được mục tiêu này. Đoạn mã sau đây cho bạn thấy cách tạo TNEF eML từ 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)
Để tạo TNEF, có thể sử dụng đoạn mã mẫu sau.
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)
Phát hiện Tin nhắn có phải là TNEF
Đoạn mã sau đây cho bạn thấy cách phát hiện xem một tin nhắn có phải là TNEF không.
from aspose.email import MailMessage
mail = MailMessage.load(data_dir + "message.eml")
is_tnef = mail.original_is_tnef
Xử lý các Tin nhắn Bị Trả Lại
Rất thường gặp trường hợp một tin nhắn gửi tới người nhận có thể bị trả lại vì bất kỳ lý do nào như địa chỉ người nhận không hợp lệ. API Aspose.Email có khả năng xử lý tin nhắn như vậy để kiểm tra xem nó có phải là email bị trả lại hay là email thông thường. Phương thức check_bounced của MailMessage trả về kết quả hợp lệ nếu tin nhắn email là email bị trả lại. Bài viết này trình bày cách sử dụng của BounceResult Lớp cung cấp khả năng kiểm tra xem một tin nhắn có phải là email bị trả lại hay không. Nó còn cung cấp thông tin chi tiết về người nhận, hành động đã thực hiện và lý do của thông báo. Đoạn mã sau đây cho bạn thấy cách xử lý các tin nhắn bị trả lại.
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()
Bộ phân tích Spam Bayes
Aspose.Email cung cấp bộ lọc email sử dụng trình phân tích spam Bayesian. Nó cung cấp SpamAnalyzer Lớp cho mục đích này. Bài viết này cho thấy cách huấn luyện bộ lọc để phân biệt giữa email rác và email thường dựa trên cơ sở dữ liệu từ.
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)