유틸리티 기능 - 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)