उपयोगिता सुविधाएँ - MailMessage

TNEF अटैचमेंट वाले MailMessages

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)