处理电子邮件中的 TNEF 附件
Contents
[
Hide
]
包含 TNEF 附件的电子邮件消息
Transport Neutral Encapsulation Format(TNEF)是 Microsoft Outlook 和 Microsoft Exchange Server 使用的专有电子邮件附件格式。Aspose.Email API 允许读取包含 TNEF 附件的电子邮件并修改其内容。随后可以将电子邮件保存为普通邮件或相同格式,同时保留这些附件。本文展示了处理包含 TNEF 附件的消息的不同代码示例,并演示如何从 Outlook MSG 文件创建 TNEF EML 文件。
识别电子邮件中的 TNEF 附件
下面的代码片段展示了如何检测电子邮件是否包含 TNEF 附件:
from aspose.email import MailMessage
mail = MailMessage.load(data_dir + "message.eml")
is_tnef = mail.original_is_tnef
读取电子邮件并保留 TNEF 附件
下面的代码片段展示了如何读取消息并保留 TNEF 附件。
from aspose.email import MailMessage, SaveOptions, EmlLoadOptions, MessageFormat, FileCompatibilityMode
options = EmlLoadOptions()
# This will Preserve the TNEF attachment as it is, file contains the TNEF attachment
options.preserve_tnef_attachments = True
eml = MailMessage.load(data_dir + "message.eml", options)
for attachment in eml.attachments:
print(attachment.name)
将 MSG 转换为 TNEF
Outlook MSG 有时包含表格和文本样式等信息,转换为 EML 时可能会损坏。将此类 MSG 文件创建为 TNEF 消息可以保留格式,并且可以通过电子邮件客户端发送时仍保持原有格式。该 convert_as_tnef 属性用于实现此功能。下面的代码片段展示了如何从 MSG 创建 TNEF 邮件。
from aspose.email.mapi import MapiMessage, MailConversionOptions, OutlookMessageFormat
mapi_msg = MapiMessage.from_file(data_dir + "message.msg")
mail_conversion_options = MailConversionOptions()
mail_conversion_options.convert_as_tnef = True
message = mapi_msg.to_mail_message(mail_conversion_options)
创建 TNEF 邮件
下面的示例代码可用于创建 TNEF 格式的电子邮件:
- 使用 MsgLoadOptions 类。
- 通过设置 options.preserve_tnef_attachments 设为 True。
- 使用 MailMessage.load(eml_file_name, options) 该方法在加载输入电子邮件文件时会应用之前配置的选项,特别确保加载的电子邮件保留与 TNEF 相关的信息(例如附件或专有的 Microsoft Outlook 格式)。
from aspose.email import MailMessage, SaveOptions, MsgLoadOptions, MessageFormat, FileCompatibilityMode
options = MsgLoadOptions()
# The PreserveTnefAttachments option with MessageFormat.Msg will create the TNEF eml.
options.preserve_tnef_attachments = True
eml = MailMessage.load(eml_file_name, options)
检测 TNEF 消息
下面的代码片段展示了如何检测消息是否为 TNEF。该 original_is_tnef 属性的 MailMessage 对象被检查以确定电子邮件最初是否为 TNEF(Transport Neutral Encapsulation Format)。如果 is_tnef 求值为 True,意味着电子邮件包含 TNEF 编码,这通常出现在来自 Microsoft Outlook 或 Exchange 的邮件中。
from aspose.email import MailMessage
mail = MailMessage.load(data_dir + "message.eml")
is_tnef = mail.original_is_tnef