עבודה עם קבצים מצורפים להודעה

ניהול קבצים מצורפים עם Aspose Outlook

יצירה ושמירה של קבצי Outlook Message (MSG) מסבירים כיצד ליצור ולשמור הודעות, וכיצד ליצור קובצי MSG עם קבצים מצורפים. מאמר זה מסביר כיצד לנהל קבצים מצורפים של Microsoft Outlook באמצעות Aspose.Email. קבצים מצורפים מקובץ הודעה נגישים ונשמרים לדיסק באמצעות תכונת Attachments של מחלקת MapiMessage. תכונת Attachments היא אוסף מסוג MapiAttachmentCollection.

בדוק אם הקובץ המצורף הוא Inline או רגיל

‘Inline’ ו-‘Regular’ קבצים מצורפים מתייחסים לאופן שבו הם משולבים בהודעת דוא"ל. קבצים מצורפים Regular הם קבצים שמצורפים בדרך המסורתית. הם בדרך כלל מוצגים ברשימה בתוך תוכנת הדוא"ל וניתן להורידם על ידי הנמען ולשמור אותם באחסון מקומי. קבצים מצורפים Inline, הידועים גם כקבצים משובצים או תמונות inline, משמשים בדרך כלל לשילוב תמונות או מדיה אחרת בתוך גוף הדוא"ל. הם אינם מוצגים ברשימה נפרדת אלא מוצגים ישירות בתוך תוכן הדוא"ל, כגון בגוף ההודעה. זה מאפשר לכלול תמונות או מדיה אחרים כחלק מתוכן ההודעה. קוד הדוגמה למטה מראה כיצד לקבוע אם קובץ מצורף הוא inline או regular:

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. כדי לשמור את הקבצים המצורפים, קרא למתודה Save() של מחלקת MapiAttachment.

קטע הקוד הבא מראה כיצד לשמור קבצים מצורפים לדיסק המקומי.

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 משובצים מופיעים גם הם באוסף Attachment של מחלקת MapiMessage. דוגמת הקוד הבאה מפענחת קובץ הודעה לחיפוש קבצים מצורפים משובצים ושומרת אותם לדיסק. המתודה הסטטית FromProperties() של MapiMessage יכולה ליצור הודעה חדשה מקובץ מצורף משובץ. קטע הקוד הבא מראה כיצד לקבל קבצים מצורפים של הודעות דואר מקוננות.

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

ניתן גם לקרוא למתודה הסטטית DestoryAttachment() של מחלקת MapiMessage. היא פועלת מהר יותר מ‑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
    # ...

הוספה והחלפה של קבצים מצורפים

API של Aspose.Email מאפשרת להכניס קבצים מצורפים במיקום מסוים בהודעה האב. היא גם מאפשרת להחליף את תוכן הקובץ המצורף בקובץ מצורף של הודעה אחרת. קטע הקוד הבא מראה כיצד לבצע הוספה והחלפה של קבצים מצורפים.

הכנסה במיקום ספציפי

API של Aspose.Email מספקת אפשרות להכניס קובץ מצורף 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 (לדוגמה) לאוסף שבו 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"