การแปลงและประมวลผลเทมเพลต Outlook (OFT)

เทมเพลต Outlook (OFT) ให้วิธีที่สะดวกในการทำให้กระบวนการส่งอีเมลซ้ำ ๆ ราบรื่นขึ้น แทนการเขียนอีเมลเดียวกันตั้งแต่ต้นทุกครั้ง คุณสามารถสร้างข้อความใน Outlook แล้วบันทึกเป็นเทมเพลต Outlook (OFT) ต่อมาทุกครั้งที่ต้องการส่งข้อความที่คล้ายกัน คุณสามารถสร้างจากเทมเพลตได้อย่างรวดเร็ว วิธีนี้ช่วยประหยัดเวลาในการเขียนเนื้อหาเดียวกันในข้อความ, กำหนดหัวเรื่อง, รูปแบบ, และอื่น ๆ.

โหลด, แก้ไข, และบันทึกไฟล์ 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 คลาส:

import aspose.email as ae

mail_message = ae.MailMessage.load("updated_message.msg")

# Send the email
with ae.clients.smtp.SmtpClient("smtp.example.com", 587, "username", "password", ae.clients.SecurityOptions.AUTO) as client:
    client.send(mail_message)

บันทึกไฟล์ MSG เป็นเทมเพลต Outlook

เพื่อสร้างอีเมลแล้วบันทึกเป็นไฟล์ OFT ให้ใช้ MailMessage คลาสนี้ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีบันทึกอีเมลเป็นเทมเพลต 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

โค้ดตัวอย่างต่อไปนี้แสดงวิธีกำหนดว่าข้อความ MAPI ที่โหลดเป็นอีเมลมาตรฐานหรือเทมเพลต Outlook (OFT):

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("message.msg")
is_oft = msg.is_template  # returns False

msg = ae.mapi.MapiMessage.load("message.oft")
is_oft = msg.is_template  # 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)