संदेश अटैचमेंट्स के साथ काम करना

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

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

जाँचें कि अटैचमेंट इनलाइन है या नियमित

"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}")

Outlook Message (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.from_file(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)

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

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

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 Outlook लाइब्रेरी Microsoft Outlook Message (.msg) फ़ाइलों से अटैचमेंट हटाने की कार्यक्षमता प्रदान करती है:

  • RemoveAttachments() मेथड को कॉल करें। यह संदेश फ़ाइल का पाथ पैरामीटर के रूप में लेता है। यह एक पब्लिक स्टैटिक मेथड के रूप में कार्यान्वित है, इसलिए आपको ऑब्जेक्ट को इंस्टैंशिएट करने की जरूरत नहीं है।

निम्नलिखित कोड स्निपेट आपको दिखाता है कि अटैचमेंट कैसे हटाए जाएँ।

import aspose.email as ae

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

आप MapiMessage क्लास की स्थैतिक मेथड DestoryAttachment() को भी कॉल कर सकते हैं। यह RemoveAttachment() से तेज़ काम करता है, क्योंकि RemoveAttachment() मेथड संदेश फ़ाइल को पार्स करता है।

import aspose.email as ae

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

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

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

MapiMessage में एक रेफ़रेंस अटैचमेंट जोड़ें

"रेफ़रेंस अटैचमेंट" सामान्यतः एक अटैचमेंट को दर्शाता है जिसमें वास्तविक फ़ाइल के बजाय एक बाहरी संसाधन का रेफ़रेंस या लिंक होता है। ये रेफ़रेंस अक्सर HTML ईमेल में बाहरी छवियों या दूरस्थ सर्वर पर होस्ट किए गए संसाधनों से लिंक करने के लिए उपयोग किए जाते हैं। पूरी फ़ाइल को एम्बेड करने के बजाय, रेफ़रेंस अटैचमेंट में एक URL या बाहरी सामग्री का रेफ़रेंस शामिल होता है।

Aspose.Email निम्नलिखित कोड नमूने में दिखाए अनुसार सही रेफ़रेंस अटैचमेंट डिस्प्ले के लिए उपकरणों का सेट प्रदान करता है:

import aspose.email as ae

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

# Add reference attachment
msg.attachments.add("Document.pdf",
    "https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
    "https://drive.google.com/drive/my-drive",
    "GoogleDrive")

# Also, you can set additional attachment properties
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_PERMISSION_TYPE, int(ae.AttachmentPermissionType.ANYONE_CAN_EDIT))
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_ORIGINAL_PERMISSION_TYPE, 0)
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_IS_FOLDER, False)
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_PROVIDER_ENDPOINT_URL, "")
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_PREVIEW_URL, "")
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_THUMBNAIL_URL, "")
# Finally save the message
msg.save("msg_with_ref_attach.msg")

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

निम्नलिखित कोड स्निपेट दिखाता है कि Outlook MSG फ़ाइलों में एक MSG फ़ाइल में एम्बेडेड PR_ATTACH_METHOD जिसका मान 5 है।

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.from_file(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 MapiAttachmentCollection की Insert मेथड का उपयोग करके एक MSG अटैचमेंट को पैरेंट MSG में सम्मिलित करने की क्षमता प्रदान करता है: 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 (उदाहरण के लिए) वाले अटैचमेंट को collection.Count = 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)

MapiMessage में अटैचमेंट का नाम बदलें

फ़ाइल से लोड किए गए ईमेल संदेशों में अटैचमेंट्स के डिस्प्ले नाम को बदलना संभव है। नीचे दिया गया कोड उदाहरण यह दिखाता है कि कैसे करें:

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"