Outlook संदेशों में अटैचमेंट्स का प्रबंधन

यह Outlook संदेश (MSG) फ़ाइलों को बनाना और सहेजना लेख ने संदेशों को बनाने और सहेजने के चरणों को समझाया, जिसमें अटैचमेंट्स वाले MSG फ़ाइलें शामिल हैं। यह लेख Aspose.Email का उपयोग करके Microsoft Outlook अटैचमेंट्स के प्रबंधन में गहराई से जाता है। यह आपको संदेश फ़ाइल से अटैचमेंट्स तक पहुंचने और उन्हें सहेजने में मार्गदर्शन करेगा, उपयोग करके attachments की प्रॉपर्टी MapiMessage class. यह प्रॉपर्टी एक कलेक्शन है जो MapiAttachmentCollection class, अटैचमेंट्स को प्रभावी ढंग से संभालने की अनुमति देता है।

इनलाइन बनाम रेगुलर अटैचमेंट्स जांचें

"Inline" और "Regular" अटैचमेंट्स ईमेल संदेश में शामिल करने के तरीके को दर्शाते हैं। Regular अटैचमेंट्स पारंपरिक तरीके से फ़ाइलें होते हैं। वे आमतौर पर ईमेल क्लाइंट में एक सूची में दिखते हैं और प्राप्तकर्ता द्वारा डाउनलोड करके स्थानीय स्टोरेज में सहेजे जा सकते हैं। Inline अटैचमेंट्स, जिसे एम्बेडेड या इनलाइन इमेजेज़ भी कहा जाता है, आमतौर पर ईमेल के बॉडी में छवियों या अन्य मीडिया को शामिल करने के लिए उपयोग होते हैं। वे अलग सूची में नहीं दिखते बल्कि सीधे ईमेल की सामग्री में, जैसे बॉडी में, दिखाए जाते हैं। यह आपको संदेश की सामग्री का हिस्सा होने वाली छवियों या अन्य मीडिया को शामिल करने देता है। नीचे दिया गया कोड नमूना दिखाता है कि अटैचमेंट इनलाइन है या रेगुलर:

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("message.msg")

for attachment in msg.attachments:
    print(f"{attachment.display_name}:{attachment.is_inline}")

MAPI संदेशों में रेफ़रेंस अटैचमेंट्स पहचानें

Aspose.Email API में शामिल है is_reference की प्रॉपर्टी MapiAttachment class जो डेवलपर्स को संदेश में रेफ़रेंस अटैचमेंट्स पहचानने में सक्षम बनाता है। नीचे दिया गया कोड नमूना देखें कि यह पायथन प्रोजेक्ट में कैसे काम करता है:

import aspose.email as ae

# Load or create a MapiMessage
msg = ae.mapi.MapiMessage.load("Message_With_Reference_Attachment.msg")

# Iterate through the attachments
for attachment in msg.attachments:
    if attachment.is_reference:
        # Process reference attachment
        pass

MSG फ़ाइलों से अटैचमेंट्स सहेजें

MSG फ़ाइल से अटैचमेंट्स सहेजने के लिए:

  1. परिचालित करें MapiAttachmentCollection कलेक्शन और व्यक्तिगत अटैचमेंट्स प्राप्त करें।
  2. अटैचमेंट्स सहेजने के लिए, कॉल करें MapiAttachment क्लास Save() विधि।

निम्न कोड स्निपेट दिखाता है कि अटैचमेंट्स को लोकल डिस्क पर कैसे सहेजें:

import aspose.email as ae

data_dir = "C://dataDir/"
file_name = "message.msg"

# Create an instance of MapiMessage from file
message = ae.mapi.MapiMessage.load(data_dir + file_name)

# Iterate through the attachments collection
for attachment in message.attachments:
    # Save the individual attachment
    attachment.save(data_dir + attachment.file_name)

TNEF अटैचमेंट्स का प्रबंधन

TNEF फ़ॉर्मेट में अटैचमेंट्स लोड और सहेजें

Aspose.Email TNEF अटैचमेंट्स को संभालने के लिए मेथड प्रदान करता है। ये मेथड्स TNEF फ़ॉर्मेट में अटैचमेंट्स को सहेजने और लोड करने की अनुमति देते हैं, जो सामान्यतः Outlook संदेशों (winmail.dat) में उपयोग होते हैं।

निम्न कोड नमूना दिखाता है कि कैसे TNEF फ़ॉर्मेट ईमेल अटैचमेंट्स को निकालें, सहेजें और पुनः जोड़ें, जबकि उन्हें मेमोरी स्ट्रीम या फ़ाइलों के माध्यम से प्रबंधित किया जाए:

import io
from aspose.email.mapi import MapiMessage, MapiAttachment

# Load the EML message (which contains a winmail.dat TNEF attachment)
msg = MapiMessage.load("message.eml")

# Save the first attachment as a winmail.dat file
msg.attachments[0].save_to_tnef("winmail.dat")

# Load the TNEF attachment from the file and add it back to the message
from_tnef_attachment = MapiAttachment.load_from_tnef("winmail.dat")
msg.attachments.add(from_tnef_attachment.long_file_name, from_tnef_attachment.binary_data)

# (Optional) If you also want to use a memory stream instead of the file
ms = io.BytesIO()
msg.attachments[0].save_to_tnef(ms)

# Reset the stream position to the beginning
ms.seek(0)

# Load the TNEF attachment directly from the memory stream and add it
from_tnef_attachment_from_stream = MapiAttachment.load_from_tnef(ms)
msg.attachments.add(from_tnef_attachment_from_stream.long_file_name, from_tnef_attachment_from_stream.binary_data)

नेस्टेड मेल संदेश अटैचमेंट्स प्राप्त करें

एम्बेडेड OLE (ऑब्जेक्ट लिंकिंग और एम्बेडिंग) अटैचमेंट्स भी अटैचमेंट कलेक्शन में दिखाई देते हैं MapiMessage class. नीचे दिया गया कोड उदाहरण दिखाता है कि संदेश फ़ाइल को कैसे पार्स करें और एम्बेडेड संदेश अटैचमेंट्स को निकालें, फिर उन्हें आगे उपयोग के लिए डिस्क पर सहेजें। यह MapiMessage class प्रदान करता है एक from_properties स्थैतिक मेथड एम्बेडेड अटैचमेंट से नया संदेश बनाने के लिए। निम्न कोड स्निपेट दिखाता है कि नेस्टेड मेल संदेश अटैचमेंट्स कैसे प्राप्त करें:

import aspose.email as ae

eml = ae.mapi.MapiMessage.load("my.msg")

# Create a MapiMessage object from the individual attachment
get_attachment = ae.mapi.MapiMessage.from_properties(eml.attachments[0].object_data.properties)

# Create an object of type MailMessageInterpreter from the above message and save the embedded message to a file on disk
mail_message = get_attachment.to_mail_message(ae.mapi.MailConversionOptions())
mail_message.save("NestedMailMessageAttachments_out.eml", ae.SaveOptions.default_eml)

अटैचमेंट्स हटाएँ

Aspose.Email लाइब्रेरी Microsoft Outlook Message (.msg) फ़ाइलों से अटैचमेंट्स हटाने की कार्यक्षमता प्रदान करती है। इसका उपयोग करें remove_attachments(path) मेथड जिसमें संदेश फ़ाइल का पाथ पैरामीटर के रूप में चाहिए। एक सार्वजनिक स्थैतिक मेथड के रूप में, आप इसे सीधे कॉल कर सकते हैं बिना किसी ऑब्जेक्ट को इंस्टैंशिएट किए, जिससे प्रक्रिया सरल और प्रभावी बनती है।

निम्न कोड स्निपेट दिखाता है कि Aspose.Email का उपयोग करके MSG फ़ाइलों से अटैचमेंट्स कैसे हटाएँ:

import aspose.email as ae

ae.mapi.MapiMessage.remove_attachments("AttachmentsToRemove_out.msg")

आप स्थैतिक destroy_attachments मेथड को भी कॉल कर सकते हैं MapiMessage class. यह remove_attachments की तुलना में तेज़ काम करता है क्योंकि remove_attachments मेथड संदेश फ़ाइल को पार्स करता है।

import aspose.email as ae

# Destroy attachments in the MapiMessage
ae.mapi.MapiMessage.destroy_attachments("AttachmentsToDestroy_out.msg")

MSG अटैचमेंट्स जोड़ें

एक Outlook संदेश में अन्य Microsoft Outlook संदेश अटैचमेंट्स के रूप में नियमित या एम्बेडेड दोनों प्रकार के हो सकते हैं। MapiAttachmentCollection के ओवरलोडेड सदस्य प्रदान करता है जोड़ें विधि Outlook संदेश बनाने के लिए, जिसमें दोनों प्रकार के अटैचमेंट्स हों।

MAPI संदेशों में रेफ़रेंस अटैचमेंट्स जोड़ें

Reference attachment आमतौर पर एक अटैचमेंट को दर्शाता है जिसमें बाहरी संसाधन का संदर्भ या लिंक होता है, न कि वास्तविक फ़ाइल स्वयं। ये रेफ़रेंसेस अक्सर HTML ईमेल में बाहरी छवियों या संसाधनों से लिंक करने के लिए उपयोग किए जाते हैं जो रिमोट सर्वर पर होस्टेड होते हैं।

MAPI संदेश में रेफ़रेंस अटैचमेंट जोड़ने के लिए, उपयोग करें add विधि MapiAttachmentCollection class और ReferenceAttachmentOptions class निर्दिष्ट करने के लिए:

  • कंटेंट URL (फ़ाइल का सीधा लिंक)

  • प्रोवाइडर URL (फ़ाइल के स्थान या UI व्यू का लिंक)

  • अटैचमेंट प्रोवाइडर नाम (उदा., "GoogleDrive")

जैसा कि नीचे दिए गए कोड नमूने में दिखाया गया है।

import aspose.email as ae

# Load or create a MapiMessage
msg = ae.mapi.MapiMessage()
msg.subject = "Email with Reference Attachment"
msg.body = "This message contains a cloud-based reference attachment."

# Create ReferenceAttachmentOptions
options = ae.mapi.ReferenceAttachmentOptions(
    "https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
    "https://drive.google.com/drive/my-drive",
    "GoogleDrive"
)

# Add the reference attachment to the message
msg.attachments.add("Document.pdf", options)

# Save the message to MSG format
msg.save("Message_With_Reference_Attachment.msg")

संदेशों को अटैचमेंट्स के रूप में एम्बेड करें

निम्न कोड स्निपेट दर्शाता है कि Outlook MSG फ़ाइलों को दूसरे MSG फ़ाइल में अटैचमेंट्स के रूप में कैसे एम्बेड करें। एम्बेडेड MSG फ़ाइल में PR_ATTACH_METHOD प्रॉपर्टी का मान 5 होता है, जो संकेत देता है कि यह "message" प्रकार का अटैचमेंट है।

import aspose.email as ae

# Create a new MapiMessage
message = ae.mapi.MapiMessage("from@test.com", "to@test.com", "Subj", "This is a message body")

# Load the attachment message
attach_msg = ae.mapi.MapiMessage.load("Message.msg")

# Add the attachment to the message
message.attachments.add("Weekly report.msg", attach_msg)

# Save the message with the embedded message attachment
message.save("WithEmbeddedMsg_out.msg")

अटैचमेंट्स से एम्बेडेड संदेश पढ़ें

निम्न कोड स्निपेट दिखाता है कि अटैचमेंट्स से एम्बेडेड संदेश कैसे पढ़ें।

import aspose.email as ae

file_name = "path/to/file.msg"

# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)

# Check if the first attachment is an Outlook message
if message.attachments[0].object_data.is_outlook_message:
    # Get the embedded message as MapiMessage
    embedded_message = message.attachments[0].object_data.to_mapi_message()
    # Perform further operations with the embedded message
    # ...

अटैचमेंट्स डालें और बदलें

Aspose.Email API पैरेंट संदेश के भीतर विशिष्ट इंडेक्स पर अटैचमेंट डालने की कार्यक्षमता प्रदान करती है। साथ ही यह मौजूदा अटैचमेंट की सामग्री को दूसरे संदेश अटैचमेंट से बदलने की क्षमता देती है। नीचे कोड स्निपेट्स दिखाते हैं कि अटैचमेंट इन्सर्ट और रिप्लेस कैसे किया जाए।

विशिष्ट स्थानों पर अटैचमेंट्स डालें

Aspose.Email API आपको एक पैरेंट MSG में MSG अटैचमेंट डालने की अनुमति देता है, Insert मेथड का उपयोग करके MapiAttachmentCollection. यह मेथड MapiAttachmentCollection Insert(int index, string name, MapiMessage msg) सिग्नेचर के साथ उपयोग किया जाता है। नीचे एक कोड स्निपेट दिया गया है जो दर्शाता है कि विशिष्ट स्थान पर अटैचमेंट कैसे डालें:

import aspose.email as ae
from io import BytesIO

file_name = "path/to/file.msg"

# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)

# Save the attachment to a memory stream
memory_stream = BytesIO()
message.attachments[2].save(memory_stream)

# Load the attachment from the memory stream
get_data = ae.mapi.MapiMessage.load(memory_stream)

# Insert the loaded attachment at index 1
message.attachments.insert(1, "new 11", get_data)

अटैचमेंट सामग्री प्रतिस्थापित करें

आप उपयोग कर सकते हैं replace एक एम्बेडेड अटैचमेंट की सामग्री को नई डेटा के साथ अपडेट करने का मेथड। तथापि, ध्यान दें कि इस मेथड का उपयोग PR_ATTACH_NUM = 4 वाले अटैचमेंट को केवल 2 अटैचमेंट वाली कलेक्शन में डालने के लिए नहीं किया जा सकता। नीचे कोड स्निपेट दिखाया गया है कि अटैचमेंट की सामग्री को कैसे बदला जाए:

import aspose.email as ae
from io import BytesIO
file_name = "path/to/file.msg"

# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)

# Save the attachment to a memory stream
memory_stream = BytesIO()
message.attachments[2].save(memory_stream)

# Load the attachment from the memory stream
get_data = ae.mapi.MapiMessage.load(memory_stream)

# Replace the attachment at index 1 with the loaded attachment
message.attachments.replace(1, "new 1", get_data)

MapiMessages में अटैचमेंट्स का नाम बदलें

Aspose.Email के साथ, आप फ़ाइल से लोड किए गए ईमेल संदेशों में अटैचमेंट्स के डिस्प्ले नाम आसानी से संशोधित कर सकते हैं। नीचे दिया गया कोड नमूना दिखाता है कि इसे कैसे प्राप्त करें:

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("message.msg")

msg.attachments[0].display_name = "New display name 1"
msg.attachments[1].display_name = "New display name 2"