Chuyển đổi và Xử lý Mẫu Outlook (OFT)
Mẫu Outlook (OFT) cung cấp cách tiện lợi để tối ưu quy trình gửi các email lặp lại. Thay vì soạn lại cùng một email từ đầu mỗi lần, bạn có thể tạo một tin nhắn trong Outlook và lưu nó dưới dạng Mẫu Outlook (OFT). Sau đó, mỗi khi cần gửi một tin tương tự, bạn có thể nhanh chóng tạo ra từ mẫu. Cách này tiết kiệm công sức viết lại nội dung, tiêu đề, định dạng và các yếu tố khác.
Tải, Sửa đổi và Lưu một Tệp OFT
Aspose.Email cung cấp một MailMessage lớp để tải và thao tác các tệp mẫu Outlook (OFT). Khi một mẫu Outlook được tải vào một thể hiện của MailMessage lớp, bạn có thể dễ dàng cập nhật các thuộc tính như người gửi, người nhận, nội dung tin nhắn, tiêu đề và nhiều thuộc tính khác.
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}")
Sau khi thực hiện các cập nhật cần thiết, bạn có thể gửi email bằng SmtpClient lớp:
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)
Lưu Tệp MSG dưới dạng Mẫu Outlook
Để tạo một email, sau đó lưu nó thành tệp OFT, sử dụng MailMessage lớp. Đoạn mã dưới đây cho thấy cách lưu một email dưới dạng Mẫu 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}")
Xác định Loại Tệp OFT và MSG
Đoạn mã dưới đây minh họa cách xác định liệu tin MAPI đã tải là tin thư chuẩn hay là một Mẫu 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
Sau khi tải tệp MSG, mã kiểm tra xem thuộc tính is_template của MapiMessaage lớp trả về True hoặc False. Nếu trả về false, tin nhắn được tải là một tin thư chuẩn, không phải Mẫu Outlook; ngược lại, nếu trả về true, tin nhắn không phải là tin thư chuẩn mà là một Mẫu Outlook.
Lưu MapiMessage hoặc MailMessage dưới dạng OFT
Cái SaveOptions là một lớp cơ sở trừu tượng cho phép người dùng chỉ định các tùy chọn bổ sung khi lưu một MailMessage thành một định dạng cụ thể. Đoạn mã dưới đây minh họa cách lưu một tin nhắn ở định dạng 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)