การจัดการไฟล์ข้อความด้วย 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.
การกำหนดเวลา Timeout สำหรับการแปลงและการโหลดข้อความ
เพื่อจำกัดเวลาเป็นมิลลิวินาทีสำหรับการแปลงข้อความ (ค่าเริ่มต้น 3 วินาที) คุณสมบัติ timeout ของ MailConversionOptions ใช้คลาส.
ต่อไปนี้คือวิธีการตั้งค่า timeout สำหรับกระบวนการแปลงและโหลดข้อความ:
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 สำหรับกระบวนการแปลงข้อความและการโหลด คุณสามารถควบคุมเวลาสูงสุดที่การดำเนินการเหล่านี้ได้รับอนุญาตให้ทำงาน หลังจากตั้งค่า 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 Template:
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 Template (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 คลาส, คลาสฐานแบบนามธรรมสำหรับคลาสที่ให้ผู้ใช้ระบุตัวเลือกเพิ่มเติมเมื่อลoad 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]}")
การสร้างข้อความต่อและตอบกลับ
Aspose.Email มีวิธีง่าย ๆ ในการสร้างข้อความต่อและตอบกลับจากข้อความที่มีอยู่
การสร้างข้อความต่อ
คุณสามารถใช้ 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 คลาสนี้ใช้เพื่อกำหนดการตั้งค่าการตอบกลับ รวมถึงข้อความต้นฉบับ, ผู้ส่ง, ผู้รับ, โหมดการตอบ, คำนำหน้าหัวเรื่อง, และเนื้อหาของข้อความตอบกลับ ข้อความตอบกลับสามารถสร้างในโหมดต่าง ๆ เช่น "Reply to Sender" หรือ "Reply to All" ตามความต้องการของคุณ คุณสามารถปรับแต่งคุณสมบัติต่าง ๆ เช่น ไฟล์แนบ, ฮีดเดอร์, และตัวเลือกการฟอร์แมตสำหรับข้อความตอบกลับ ตัวอย่างโค้ดด้านล่างแสดงวิธีสร้างข้อความส่งต่อ:
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")