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 プロパティをチェックします MapiMessage クラスは 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)