ניהול קבצים מצורפים בהודעות Outlook
ה יצירת ושמירת קבצי הודעות Outlook (MSG) המאמר הסביר את השלבים המעורבים ביצירת ושמירת הודעות, כולל קבצי MSG עם קבצים מצורפים. המאמר מתעמק בניהול קבצים מצורפים של Microsoft Outlook באמצעות Aspose.Email. הוא ידריך אתכם בגישה ושמירת קבצים מצורפים מקובץ הודעה תוך שימוש ב- attachments מאפיין של MapiMessage class. מאפיין זה הוא אוסף השייך ל- MapiAttachmentCollection class, מאפשר טיפול יעיל בקבצים מצורפים.
בדיקת Inline מול Regular בקבצים מצורפים
‘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}")
זיהוי קבצים מצורפים כייחוס (Reference) בהודעות MAPI
ה-API של Aspose.Email כולל את ה- is_reference מאפיין של MapiAttachment class המאפשר למפתחים לזהות קבצים מצורפים כניתן הפנייה (reference) בהודעה. נסו את דוגמת הקוד למטה כדי לראות כיצד זה עובד בפרויקט פייתון:
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:
- איטרציה דרך ה- MapiAttachmentCollection אוסף וקבלו את הקבצים המצורפים האישיים.
- כדי לשמור את הקבצים המצורפים, קראו ל- 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).
- static MapiAttachment MapiAttachment.load_from_tnef(string fileName) – טוען קובץ מצורף מקובץ TNEF.
- static MapiAttachment MapiAttachment.load_from_tnef(Stream stream) – טוען קובץ מצורף מזרם TNEF.
- void MapiAttachment.save_to_tnef(string filename) – שומר קובץ מצורף לקובץ TNEF.
- void MapiAttachment.save_to_tnef(Stream stream) – שומר קובץ מצורף לזרם TNEF.
דוגמת הקוד הבאה מציגה כיצד לחלץ, לשמור ולשוב להוסיף קבצים מצורפים בפורמט 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 משובץ (Object Linking and Embedding) גם הם מופיעים באוסף הקבצים המצורפים של ה- 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) מתודה הדורשת את הנתיב של קובץ ההודעה כפרמטר שלה. כמתודה סטטית ציבורית, ניתן לקרוא לה ישירות دون צורך ליצירת מופע, מה שהופך את התהליך לפשוט ויעיל.
קטע הקוד הבא מציג כיצד להסיר קבצים מצורפים מקבצי MSG באמצעות Aspose.Email:
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 יכולה להכיל הודעות Outlook אחרות בקבצים מצורפים, בין אם רגילים ובין אם משובצים. ה- MapiAttachmentCollection מספקת חברים מעודפים של ה- להוסיף מתודה ליצירת הודעות Outlook עם שני סוגי הקבצים המצורפים.
נסו זאת!
הוסף או הסר קבצים מצורפים למייל אונליין עם ה‑ Aspose.Email Editor App.
הוספת קבצים מצורפים כייחוס להודעות MAPI
קובץ מצורף כייחוס מתייחס בדרך כלל לקובץ מצורף שמכיל הפניה או קישור למשאב חיצוני במקום הקובץ עצמו. הפניות אלו משמשות לעיתים במיילים בפורמט HTML כדי לקשר לתמונות חיצוניות או משאבים המאוחסנים בשרת מרוחק. במקום לשבץ את הקובץ כולו, קובץ מצורף כייחוס כולל כתובת URL או הפניה לתוכן החיצוני.
להוספת קובץ מצורף כייחוס (reference) להודעת 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
# ...
הוספה והחלפה של קבצים מצורפים
ה-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 למאגר שמכיל רק 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"