ניהול קבצי הודעות עם Aspose.Email.Outlook
קביעת סוג פריט MAPI בתיקיית PST
קובץ PST בדרך כלל מכיל סוגים שונים של נתונים, כגון הודעות דוא"ל, אירועי לוח שנה, אנשי קשר ועוד. ה‑ MapiItemType מחלקה של Aspose.Email מאפשרת לגשת ולסווג סוגים שונים של פריטי MAPI בקובץ PST. הקוד למטה מדגים כיצד לקבוע את סוג פריט MAPI בתיקיית PST:
import aspose.email as ae
pst = ae.storage.pst.PersonalStorage.from_file("test.pst")
folder = pst.root_folder.get_sub_folder("Calendar")
for messageInfo in folder.enumerate_messages():
msg = pst.extract_message(messageInfo)
# Get the class type based on msg.SupportedType
item_type = msg.supported_type
# Non-supported type. It cannot be accessed as appropriate item type.
if item_type == ae.mapi.MapiItemType.NONE:
print("Item type not supported")
# An email message.
elif item_type == ae.mapi.MapiItemType.MESSAGE:
# You can access to MapiMessage properties there.
# A subject for example
print(msg.subject)
# A contact item. Can be accessed as MapiContact.
elif item_type == ae.mapi.MapiItemType.CONTACT:
contact = msg.to_mapi_message_item()
# You can access to MapiContact properties there.
# A name_info.display_name for example.
print(contact.name_info.display_name)
# A calendar item. Can be accessed as MapiCalendar.
elif item_type == ae.mapi.MapiItemType.CALENDAR:
calendar = msg.to_mapi_message_item()
# You can access to MapiCalendar properties there.
# A location for example.
print(calendar.location)
# A distribution list. Can be accessed as MapiDistributionList.
elif item_type == ae.mapi.MapiItemType.DIST_LIST:
dlist = msg.to_mapi_message_item()
# You can access to MapiDistributionList properties there
# A Journal entry. Can be accessed as MapiJournal.
elif item_type == ae.mapi.MapiItemType.JOURNAL:
journal = msg.to_mapi_message_item()
# You can access to MapiJournal properties there
# A StickyNote. Can be accessed as MapiNote.
elif item_type == ae.mapi.MapiItemType.NOTE:
note = msg.to_mapi_message_item()
# You can access to MapiNote properties there
# A Task item. Can be accessed as MapiTask.
elif item_type == ae.mapi.MapiItemType.TASK:
task = msg.to_mapi_message_item()
# You can access to MapiTask properties there
המרת MSG להודעת MIME
API של Aspose.Email מאפשרת המרת קובץ MSG להודעת MIME בעזרת המתודה ToMailMessage.
קביעת תזמון לפעולת המרה וטעינת הודעה
כדי להגביל את הזמן במילישניות של המרת ההודעה (ערך ברירת המחדל 3 שניות), המאפיין timeout של ה- MailConversionOptions class משמשת.
הנה כיצד ניתן להגדיר זמן קצוב לתהליכי המרת הודעות וטעינתן:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
options = ae.mapi.MailConversionOptions()
options.timeout = 5000
mailMessage = msg.to_mail_message(options)
על ידי הגדרת זמן קצוב (timeout) לתהליכי המרת הודעות וטעינתן, ניתן לשלוט בזמן המרבי שמותר לתהליכים אלו לרוץ. לאחר קביעת זמן הקצוב, ניתן להמשיך בתהליכי המרת והטעינה של ההודעות.
עיבוד קבצי Outlook Template (OFT)
טעינה, שינוי ושמירת קובץ OFT
תבניות Outlook (OFT) מספקות דרך נוחה לייעל תהליך שליחת הודעות אימייל חוזרות. במקום לכתוב את אותה הודעה מאפס בכל פעם, ניתן ליצור הודעה ב‑Outlook ולשמור אותה כתבנית Outlook (OFT). לאחר מכן, כשצריך לשלוח הודעה דומה, ניתן ליצור אותה במהירות מהתבנית. גישה זו חוסכת את המאמץ של כתיבת תוכן זהה בגוף ההודעה, קביעת שורת הנושא, העיצוב ועוד.
של Aspose.Email MailMessage המחלקה מספקת כלי חזק לטעון ולתפעל קבצי תבנית Outlook (OFT). לאחר שתבנית Outlook נטענת למופע של מחלקת MailMessage, ניתן לעדכן בקלות תכונות כגון השולח, הנמען, גוף ההודעה, נושא ועוד מאפיינים אחרים.
import aspose.email as ae
# Load the OFT file
oft_file_path = "your_template.oft"
mail_message = ae.MailMessage.load(oft_file_path)
# Update properties as needed
mail_message.subject = "Updated Subject"
mail_message.body = "Updated body text."
# Save the updated message as an MSG file
msg_file_path = "updated_message.msg"
mail_message.save(msg_file_path, ae.MailMessageSaveType.outlook_message_format_unicode)
print(f"Updated message saved to {msg_file_path}")
לאחר ביצוע העדכונים הנדרשים, ניתן לשלוח את הדוא"ל באמצעות ה‑ SmtpClient מחלקה:
# Send the email
smtpClient.send(message)
שמירת קובץ Outlook MSG כתבנית
לצורך זה, ניתן להשתמש ב‑ MailMessage מחלקה ליצירת אימייל, ולאחר מכן לשמור אותו כקובץ OFT. דוגמת הקוד שלהלן תציג כיצד לשמור אימייל כתבנית Outlook:
import aspose.email as ae
# Create a new MailMessage
message = ae.MailMessage()
message.subject = "Sample Outlook Template"
message.html_body = "<html><body>This is the body of the email.</body></html>"
message.from_address = ae.MailAddress("your.email@example.com", "Your Name")
# Save the MailMessage as an Outlook Template (OFT) file
oft_file_path = "sample_template.oft"
message.save(oft_file_path, ae.SaveOptions.default_oft)
print(f"Outlook Template saved as {oft_file_path}")
OFT או MSG: קביעת סוג של MapiMessage
קוד הדוגמה הבא מציג כיצד לקבוע אם הודעת MAPI שנטענה היא הודעת אימייל רגילה או תבנית Outlook (OFT):
msg = ae.mapi.MapiMessage.Load("message.msg");
isOft = msg.is_template # returns false
msg = ae.mapi.MapiMessage.Load("message.oft");
isOft = msg.IsTemplate; # returns true
לאחר טעינת קובץ MSG, הקוד בודק האם המאפיין is_template של ה‑ MapiMessaage המחלקה היא True או False. אם היא מחזירה false, ההודעה הטעונה היא הודעת דוא"ל סטנדרטית, ולא תבנית Outlook; אם היא מחזירה true, ההודעה אינה הודעת דוא"ל סטנדרטית, אלא תבנית Outlook.
שמירת MapiMessage או MailMessage כ‑OFT
ה SaveOptions הוא מחלקת בסיס אבסטרקטית למחלקות המאפשרת למשתמש לציין אפשרויות נוספות בעת שמירת MailMessage בפורמט מסוים. דוגמת הקוד שלהלן מדגימה כיצד לשמור הודעה בפורמט OFT:
import aspose.email as ae
# Save the MailMessage to OFT format
eml = ae.MailMessage.load("message.eml")
eml.save("message.oft", ae.SaveOptions.default_oft)
# or alternative way 2
save_options = ae.MsgSaveOptions(ae.MailMessageSaveType.outlook_template_format)
eml.save("message.oft", save_options)
# or alternative way 3
save_options = ae.SaveOptions.create_save_options(ae.MailMessageSaveType.outlook_template_format)
eml.save("message.oft", save_options)
# Save the MapiMessage to OFT format
msg = ae.mapi.MapiMessage.load("message.msg")
msg.save("message.oft", ae.SaveOptions.default_oft)
# or alternative way 2
save_options = ae.MsgSaveOptions(ae.MailMessageSaveType.outlook_template_format)
msg.save("message.oft", save_options)
# or alternative way 3
save_options = ae.SaveOptions.create_save_options(ae.MailMessageSaveType.outlook_template_format)
msg.save("message.oft", save_options)
ניהול הודעות עם חתימות דיגיטליות
הספרייה מספקת את LoadOptions מחלקה, מחלקת בסיס אבסטרקטית למחלקות המאפשרת למשתמש לציין אפשרויות נוספות בעת טעינת MailMessage מפורמט מסוים. אפשרות שמירת החתימה מוגדרת כברירת מחדל.
המרה מ‑EML ל‑MSG תוך שמירה על החתימה
דוגמת הקוד למטה מציגה כיצד לטעון הודעה, להמיר אותה לפורמט MSG ולשמור כ‑MSG (החתימה נשמרת כברירת מחדל):
import aspose.email as ae
# Load mail message
loadOptions = ae.EmlLoadOptions()
message = ae.MailMessage.load("Message.eml", loadOptions)
# Save as MSG
message.save("ConvertEMLToMSG_out.msg", ae.SaveOptions.default_msg_unicode)
המרת הודעות S/MIME מ‑MSG ל‑EML
Aspose.Email מאפשרת להמיר מ‑MSG ל‑EML תוך שמירה על חתימה דיגיטלית כפי שמופיע בקטע הקוד הבא.
import aspose.email as ae
# Load mail message
loadOptions = ae.EmlLoadOptions()
smime_eml = ae.MailMessage.load("smime.eml", loadOptions)
conversion_options = ae.mapi.MapiConversionOptions(ae.mapi.OutlookMessageFormat.UNICODE)
msg = ae.mapi.MapiMessage.from_mail_message(smime_eml, conversion_options)
# Save File to disk
msg.save("ConvertMIMEMessagesFromMSGToEML_out.msg")
הגדרת קטגוריית צבע לקבצי Outlook MSG
לעיתים יתכן ותצטרכו להבדיל בין מיילים בעלי חשיבות מיוחדת ולארגן אותם ויזואלית. הספרייה מספקת דרך לעשות זאת על ידי הקצאת צבע ספציפי לפריט ההודעה. כאשר אתם מגדירים קטגוריית צבע לפריט, זה מאפשר לזהות ולאתר פריטים קשורים במבט אחד. השתמשו ב- FollowUpManager מחלקה לקביעת קטגוריית צבע להודעה כפי שמוצג בדוגמת הקוד הבאה:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
# Add Two categories
ae.mapi.FollowUpManager.add_category(msg, "Purple Category")
ae.mapi.FollowUpManager.add_category(msg, "Red Category")
# Retrieve the list of available categories
categories = ae.mapi.FollowUpManager.get_categories(msg)
# Remove the specified category and then Clear all categories
ae.mapi.FollowUpManager.remove_category(msg, "Red Category")
ae.mapi.FollowUpManager.clear_categories(msg)
גישה למידע Follow Up מקובץ MSG
חילוץ מידע על קבלה והקראה
דוגמת הקוד למטה מציגה כיצד לחלץ מידע על נמען והמצב של המעקב שלו מקובץ הודעת Outlook (MSG):
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("received.msg")
for recipient in msg.recipients:
print(f"Recipient: {recipient.display_name}")
print(f"Delivery time: {recipient.properties[ae.mapi.MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME_DELIVERY]}")
print(f"Read time: {recipient.properties[ae.mapi.MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME_READ]}")
יצירת הודעות Forward ו‑Reply
Aspose.Email מספקת דרכים פשוטות ליצור הודעות הודעה קידום (forward) ותשובה (reply) בהתבסס על קיימות.
יצירת הודעת Forward
ניתן להשתמש ב- ForwardMessageBuilder מחלקה ליצירת הודעת העברה על‑ידי הגדרת הודעת המקור, השולח, הנמענים, הנושא והגוף. הודעות העברה יכולות לכלול את ההודעה המקורית כקובץ מצורף או כחלק מהגוף של ההודעה המועברת. יש לך גמישות להתאים תכונות נוספות כגון קבצים מצורפים, כותרות ואופציות עיצוב. דוגמת הקוד למטה מציגה כיצד ליצור הודעת העברה:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("original.msg")
builder = ae.tools.ForwardMessageBuilder()
builder.addition_mode = ae.tools.OriginalMessageAdditionMode.TEXTPART
forwardMsg = builder.build_response(msg)
forwardMsg.save("forward_out.msg")
יצירת הודעת תשובה
ה ReplyMessageBuilder מחלקה משמשת להגדרת הגדרות תגובה, כולל הודעת המקור, השולח, הנמענים, מצב התגובה, קידומת הנושא, וגוף הודעת התגובה. ניתן ליצור הודעות תגובה במצבי תגובה שונים כגון "תגובה לשולח" או "תגובה לכל" בהתאם לדרישה. ניתן להתאים מגוון תכונות כגון קבצים מצורפים, כותרות, ואופציות עיצוב עבור הודעת התגובה. דוגמת הקוד למטה מציגה כיצד ליצור הודעת העברה:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("original.msg")
builder = ae.tools.ReplyMessageBuilder()
# Set ReplyMessageBuilder Properties
builder.reply_all = True
builder.addition_mode = ae.tools.OriginalMessageAdditionMode.TEXTPART
builder.response_text = "<p><b>Dear Friend,</b></p> I want to do is introduce my co-author and co-teacher. <p><a href=\"www.google.com\">This is a first link</a></p><p><a href=\"www.google.com\">This is a second link</a></p>";
replyMsg = builder.build_response(msg)
replyMsg.save("reply_out.msg")