转换和处理 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)