加载和保存电子邮件消息
加载电子邮件消息
从 EML 加载
本节介绍如何使用该方式将 EML 文件加载到 MailMessage 对象中。 EmlLoadOptions 类。EmlLoadOptions 类提供多种选项,以自定义 EML 文件的加载方式,例如保留嵌入式消息格式或控制 TNEF 附件加载行为。
- 初始化 EmlLoadOptions 实例。
- 根据需要配置加载选项。
- 使用 MailMessage.Load() 使用指定选项加载 EML 文件的方法。
using Aspose.Email;
// Initialize EmlLoadOptions
var loadOptions = new EmlLoadOptions
{
// Configure load options
PreferredTextEncoding = Encoding.UTF8, // Sets preferred encoding for the message
PreserveEmbeddedMessageFormat = true, // Preserve format of embedded messages
PreserveTnefAttachments = true, // Control TNEF attachment loading
RemoveSignature = false // Do not remove the signature
};
// Load the EML file
var eml = MailMessage.Load("file.eml", loadOptions);
EmlLoadOptions 的属性
- PreferredTextEncoding:设置消息主题和正文的首选编码。默认值为
null. - PreserveEmbeddedMessageFormat:指示加载时是否保留嵌入式消息的格式。默认值为
false. - PreserveTnefAttachments:控制 TNEF 附件加载行为。默认值为
false. - RemoveSignature:指定加载时是否应删除签名。默认值为
false.
保留嵌入式消息格式
加载带签名/不带签名的消息
默认情况下加载 EML 文件时会保留签名。若要移除签名,可设置 LoadOptions.RemoveSignature 属性设为 true。
下面的代码示例展示了在加载消息时如何移除签名:
var msg = MailMessage.Load(fileName, new EmlLoadOptions() { RemoveSignature = true});
从 EMLX 加载
以下章节介绍使用以下方式加载 EMLX 文件 EmlxLoadOptions 类。此类提供类似于 EmlLoadOptions 的选项,能够控制编码、签名移除等。
- 实例化 EmlxLoadOptions。
- 根据需要配置属性。
- 使用 MailMessage.Load() 加载 EMLX 文件的方法。
using Aspose.Email;
// Instantiate EmlxLoadOptions
var loadOptions = new EmlxLoadOptions
{
// Configure load options
PreferredTextEncoding = Encoding.UTF8, // Set encoding preference
PreserveEmbeddedMessageFormat = true, // Preserve embedded message formats
RemoveSignature = true // Remove signatures during loading
};
// Load the EMLX file
var emlx = MailMessage.Load("file.emlx", loadOptions);
EmlxLoadOptions 的属性
- PreferredTextEncoding:设置消息主题和正文的首选编码。默认值为
null. - PreserveEmbeddedMessageFormat:指示是否保留嵌入式消息的格式。默认值为
false. - RemoveSignature:指定加载时是否删除签名。默认值为
false.
从 HTML 加载
了解如何使用以下方式将 HTML 文件加载到 MailMessage 中 HtmlLoadOptions 类。此类专为处理 HTML 内容而设计,提供管理资源和添加纯文本视图的选项。
- 初始化 HtmlLoadOptions 的实例。
- 配置必要的属性。
- 使用 MailMessage.Load() 使用指定选项加载 HTML 文件的方法。
using Aspose.Email;
// Initialize HtmlLoadOptions
var loadOptions = new HtmlLoadOptions
{
// Configure load options
PathToResources = "resources/", // Path to directory containing resource files
PreferredTextEncoding = Encoding.UTF8, // Set encoding preference
ShouldAddPlainTextView = true // Add plain text view of the body
};
// Load the HTML file
var html = MailMessage.Load("file.html", loadOptions);
HtmlLoadOptions 的属性
- PathToResources:定义包含资源文件(例如图像)的目录路径。
- PreferredTextEncoding:设置消息主题和正文的首选编码。默认值为
null. - PreserveEmbeddedMessageFormat:确定是否保留嵌入消息的格式。默认值为
false. - ShouldAddPlainTextView:指定是否添加消息正文的纯文本视图。默认值为
false.
从 MHTML 加载
本节说明如何使用以下方式加载 MHTML 文件 MhtmlLoadOptions 类。MhtmlLoadOptions 类提供管理编码、保留嵌入消息格式以及处理 TNEF 附件的选项。
- 创建 MhtmlLoadOptions 的实例。
- 配置所需属性。
- 使用以下方式加载 MHTML 文件: MailMessage.Load() 方法。
using Aspose.Email;
// Create MhtmlLoadOptions
var loadOptions = new MhtmlLoadOptions
{
// Set load options
PreferredTextEncoding = Encoding.UTF8, // Set encoding preference
PreserveEmbeddedMessageFormat = true, // Preserve format of embedded messages
PreserveTnefAttachments = true, // Handle TNEF attachments
RemoveSignature = false // Keep the signature
};
// Load the MHTML file
var mhtml = MailMessage.Load("file.mht", loadOptions);
MhtmlLoadOptions 的属性
- MessageFormat:表示邮件消息的格式,可为 EML、MSG 或 MHTML。默认值为 EML。
- PreferredTextEncoding:设置消息主题和正文的首选编码。默认值为
null. - PreserveEmbeddedMessageFormat:确定是否保留嵌入消息的格式。默认值为
false. - PreserveTnefAttachments:控制 TNEF 附件加载行为。默认值为
false. - RemoveSignature:指定加载时是否删除签名。默认值为
false.
从 MSG 加载
本节说明如何使用以下方式将 MSG 文件加载到 MailMessage 对象中 MsgLoadOptions 类。MsgLoadOptions 类提供多种属性来处理 MSG 文件的加载方式,包括保留 RTF 内容或管理 TNEF 附件的选项。
- 创建 MsgLoadOptions 的实例。
- 设置所需属性以自定义加载。
- 使用以下方式加载 MSG 文件: MailMessage.Load() 方法。
using Aspose.Email;
// Create MsgLoadOptions
var loadOptions = new MsgLoadOptions
{
// Set load options
KeepOriginalEmailAddresses = true, // Preserve original email addresses
PreferredTextEncoding = Encoding.UTF8, // Set preferred encoding
PreserveRtfContent = true, // Keep RTF content in the message
PreserveTnefAttachments = true, // Handle TNEF attachments
RemoveSignature = false, // Keep the signature
Timeout = 5000 // Set timeout to 5 seconds
};
// Load the MSG file
var msg = MailMessage.Load("file.msg", loadOptions);
MsgLoadOptions 的属性
- KeepOriginalEmailAddresses:指示是否保留原始电子邮件地址。默认值为
false. - PreferredTextEncoding:定义消息主题和正文的首选编码。默认值为
null. - PreserveEmbeddedMessageFormat:确定是否应保留嵌入消息的格式。默认值为
false. - PreserveRtfContent:指定是否在中保留 RTF 正文内容
MailMessage。默认值为false. - PreserveTnefAttachments:管理 TNEF 附件的加载行为。默认值为
false. - RemoveSignature:决定是否在加载时删除签名。默认值为
false. - 超时:限制转换期间的格式化时间(毫秒)。默认值为 3000 毫秒。
从 TNEF 加载
了解如何使用以下方式加载 TNEF eml 文件 TnefLoadOptions 类。此类提供在加载过程中管理编码和删除签名的选项。
- 实例化 TnefLoadOptions。
- 根据需要配置属性。
- 使用 MailMessage.Load() 加载 TNEF 文件的方法。
using Aspose.Email;
// Instantiate TnefLoadOptions
var loadOptions = new TnefLoadOptions
{
// Configure load options
PreferredTextEncoding = Encoding.UTF8, // Set encoding preference
PreserveEmbeddedMessageFormat = true, // Preserve embedded message formats
RemoveSignature = true // Remove signatures during loading
};
// Load the TNEF file
var tnef = MailMessage.Load("file.eml", loadOptions);
TnefLoadOptions 的属性
- MessageFormat:表示邮件消息的格式,例如 EML、MSG 或 MHTML。默认值为 EML。
- PreferredTextEncoding:设置消息主题和正文的首选编码。默认值为
null. - PreserveEmbeddedMessageFormat:指示是否保留嵌入式消息的格式。默认值为
false. - RemoveSignature:指定加载时是否删除签名。默认值为
false.
检测文件格式
Aspose.Email API 提供检测提供的邮件文件格式的功能。该 DetectFileFormat 方法的 FileFormatUtil 可以使用该类来实现此功能。以下类和方法可用于检测已加载的文件格式。
- FileFormatType 类
- FileFormatInfo 类
- FileFormatUtil 类
- FileFormatUtil.DetectFileFormat(Stream) Method
- FileFormatUtil.DetectFileFormat(String) Method
以下代码片段展示了如何检测文件格式。
保存并转换消息
Aspose.Email 让将任何消息类型转换为其他格式变得轻松。为演示此功能,本文的代码从磁盘加载三种类型的消息并将它们保存为其他格式。基类 SaveOptions 以及这些类 EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions 用于保存时的额外设置 MailMessage 可用于将消息保存为其他格式。本文展示了如何使用这些类将示例电子邮件保存为:
- EML 格式。
- Outlook MSG。
- MHTML 格式。
- HTML 格式。
保存为 EML
以下代码片段展示了如何加载 EML 消息并以相同格式保存到磁盘。
保留原始边界
以下代码片段展示了如何加载 EML 并保存为 EML,同时保留原始边界。
保留 TNEF 附件
以下代码片段展示了如何保存为 EML,同时保留 TNEF 附件。
将 EML 保存为 MSG
下面的代码片段展示了如何加载 EML 消息并使用 SaveOptions.
保留日期
该 MsgSaveOptions 类允许您将源消息保存为 Outlook 消息文件(MSG),并保留日期。以下代码片段展示了如何以保留日期的方式保存为 MSG。
将 EML 保存为 MHTML
可以使用不同的 MHTML 选项来获得期望的结果。以下代码片段展示了如何将 EML 消息加载到 MailMessage 并将其转换为带有 UTC 系统消息日期的 MHTML。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Set options for MHTML output
MhtSaveOptions saveOptions = SaveOptions.DefaultMhtml;
saveOptions.PreserveOriginalDate = false; // save a message date as UTC date
// Initialize and load an existing EML file
using (MailMessage mailMessage = MailMessage.Load(folderPath + "Message.eml"))
{
mailMessage.Save(folderPath + "Message_out.mhtml", saveOptions);
}
转换选项
该 MhtSaveOptions 该类提供将电子邮件消息保存为 MHTML 格式的额外选项。枚举 MhtFormatOptions 使得可以将额外的电子邮件信息写入输出 MHTML。可以写入以下附加字段:
WriteHeader– 将电子邮件标头写入输出文件。WriteOutlineAttachments– 将大纲附件写入输出文件。WriteCompleteEmailAddress– 将完整的电子邮件地址写入输出文件。NoEncodeCharacters– 不应使用字符的传输编码。HideExtraPrintHeader– 隐藏输出文件顶部的额外打印标题。WriteCompleteToEmailAddress– 将收件人的完整电子邮件地址写入输出文件。WriteCompleteFromEmailAddress– 将发件人的完整电子邮件地址写入输出文件。WriteCompleteCcEmailAddress– 将所有抄送收件人的完整电子邮件地址写入输出文件。WriteCompleteBccEmailAddress– 将任何密送收件人的完整电子邮件地址写入输出文件。RenderCalendarEvent– 将日历事件的文本写入输出文件。SkipByteOrderMarkInBody– 将字节顺序标记(BOM)写入输出文件。RenderVCardInfo– 将 VCard AlternativeView 的文本写入输出文件。DisplayAsOutlook– 显示 From 标头。RenderTaskFields– 将特定任务字段写入输出文件。None– 未指定设置。
以下代码片段展示了如何使用可选设置将 EML 文件转换为 MHTML。
渲染日历事件
该 MhtFormatOptions.RenderCalendarEvent 将日历事件呈现为输出的 MHTML。以下代码片段展示了在转换为 MHTML 时如何渲染日历事件。
导出为不含内联图片的 MHT
使用自定义时区导出电子邮件为 MHT
MailMessage class 提供了 TimeZoneOffset 属性用于在导出为 MHT 时设置自定义时区。以下代码片段展示了如何使用自定义时区导出电子邮件为 MHT。
字体调整
将 EML 保存为 HTML
该 HtmlSaveOptions 类允许您将消息正文导出为 HTML。以下代码片段展示了如何将消息保存为 HTML。
不保存嵌入资源
将 EML 保存为 OFT
以下代码片段展示了如何将消息保存为 Outlook 模板(.oft)文件。
将 EML 转换为 MSG(MailMessage 到 MapiMessage)
要转换一个 MailMessage 从 EML 文件加载的对象转换为一个 MapiMessage 对象,使用 MapiConversionOptions 类。此转换通常是必要的,因为这两个对象服务于不同的目的,在电子邮件处理方面满足不同的需求。 MapiMessage 专为与 Microsoft Outlook 和 Exchange Server 兼容而设计,因为它遵循消息应用程序编程接口(MAPI)格式。这确保转换为 MSG 文件的电子邮件与 Microsoft 产品完全兼容,能够直接在 Outlook 中打开、修改和管理。
当电子邮件需要归档、索引或由需要 MAPI 格式、PST/OST、OLM 存储的系统处理时,此转换也是必需的。例如, MapiConversionOptions 该类允许您指定其他选项,例如在转换过程中保留原始消息格式、保留原始日期以及压缩 RTF 正文。以下代码示例展示了如何从 .eml 格式的文件加载电子邮件并将其转换为 Outlook .msg 格式,允许自定义,但确保在转换过程中保留原始电子邮件的某些特性。
- 创建以下实例:
MapiConversionOptions并配置其属性。 - 转换
MailMessage对象到一个MapiMessage使用已配置的转换选项的对象。
MailMessage eml = MailMessage.Load("email.eml", new EmlLoadOptions());
// Create an instance of MapiConversionOptions
var conversionOptions = new MapiConversionOptions
{
// Configure MapiConversionOptions
Format = OutlookMessageFormat.Unicode, // Use Unicode format for MSG
PreserveEmbeddedMessageFormat = true, // Preserve the format of embedded messages
PreserveOriginalAddresses = true, // Keep original email addresses
PreserveOriginalDates = true, // Preserve original dates
PreserveEmptyDates = false, // Generate new dates if original are empty
RemoveSignature = false, // Do not remove the signature
UseBodyCompression = true, // Enable RTF body compression
ForcedRtfBodyForAppointment = false // Disable forced RTF body for appointments
};
// Convert MailMessage to MapiMessage
MapiMessage msg = MapiMessage.FromMailMessage(eml, conversionOptions);
MapiConversionOptions 的可用属性
-
ASCII格式:返回
MapiConversionOptions已配置为OutlookMessageFormat作为 ASCII,带有PreserveSignature设置为 false 并且UseBodyCompression设置为 false。当您需要纯 ASCII 格式转换时,此选项很有用。 -
UnicodeFormat:返回
MapiConversionOptions与OutlookMessageFormat设置为 Unicode,且两者PreserveSignature和UseBodyCompression设置为 false。使用此选项可在 MSG 文件中获得更全面的字符支持。 -
ForcedRtfBodyForAppointment:设置为 true 时,强制日历约会使用 RTF 正文。这对确保兼容某些邮件客户端很有用。默认值为 true。
-
Format:指定 MSG 文件的输出格式。可以设置为
OutlookMessageFormat.ASCII或OutlookMessageFormat.Unicode,控制转换后的 MSG 使用 ASCII 还是 Unicode 编码。 -
PreserveEmbeddedMessageFormat:确定在转换为时是否保留嵌入消息的原始 EML 格式
MapiMessage。将此设置为 true 可确保嵌入的电子邮件保留原始结构。默认值为 false。 -
PreserveEmptyDates:设置为 true 时,保留 EML 中的原始保存和修改日期。否则,如果原始日期为空,将生成新的日期。
-
PreserveOriginalAddresses:如果设置为 true,则在不进行验证的情况下保留原始电子邮件地址。此功能在处理可能不符合严格标准但需原样保留的电子邮件地址时很有用。默认值为 false。
-
PreserveOriginalDates:确保在转换过程中保留原始的发送和接收日期。将此设置为 true 可保留这些日期,以获得更好的历史准确性。
-
RemoveSignature:控制在转换过程中是否删除数字签名(如果存在)。默认值为 false,表示签名会被保留,除非明确移除。
-
UseBodyCompression:如果设置为 true,则启用 RTF 正文压缩。这有助于减小 MSG 文件大小,特别是在处理大型富格式消息时。