在这个简单的教程中,我们将学习如何使用 C#** 将 Outlook Email 转换为 PDF 以及配置环境的详细步骤。您将看到,为了将 MSG 转换为 PDF,可以使用基于 C# 的 API,并且您可以在任何常用操作系统(如 Windows、macOS 或 Linux)中使用此示例。
使用 C# 将 Outlook 电子邮件转换为 PDF 的步骤
- 从 Visual Studio 中的 NuGet 包管理器工具安装 Aspose.Email 和 Aspose.Words 库
- 初始化 MailMessage 类对象以加载 MSG 或 EML 格式的电子邮件文件
- 将加载的电子邮件 (MSG/EML) 文件作为流保存为中间 MHTML 格式
- 将 LoadFormat 设置为 MHTML 并初始化 Document 类对象以加载在上一步中创建的 MHTML
- 通过使用 C# EML 到 PDF 转换中的 Save 方法完成
通过使用C# MSG 到 PDF 转换应用程序中的上述步骤开发。我们将在第一步中使用 MailMessage 类对象加载 MSG 或 EML 文件。然后我们将加载的电子邮件文件以中间 MHTML 格式保存到流中。在后续步骤中,我们将使用 Document 类对象加载中间 MHTML。最后将 EML 转换为 PDF C# 将使用保存方法并将 PDF 保存在磁盘上。
使用 C# 将 Outlook 电子邮件转换为 PDF 的代码
using System; | |
using System.IO; | |
using Aspose.Email; | |
using Aspose.Words; | |
namespace WordKB | |
{ | |
public class EmailToPDF | |
{ | |
public static void EmailToPDFExample() | |
{ | |
// Applying product license to read the Email (MSG/EML) file | |
Aspose.Email.License EmailLic = new Aspose.Email.License(); | |
EmailLic.SetLicense("Aspose.Total.lic"); | |
// Applying product license to convert MHTML to PDF | |
Aspose.Words.License WordsLic = new Aspose.Words.License(); | |
WordsLic.SetLicense("Aspose.Total.lic"); | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
// Load the MSG or EML file | |
MailMessage message = MailMessage.Load("Message.msg"); | |
message.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml); | |
// Resest the Memory stream position | |
ms.Position = 0; | |
// Instantiate LoadOptions to set the LoadFormat to Mhtml | |
Aspose.Words.Loading.LoadOptions loadOptions = new Aspose.Words.Loading.LoadOptions(); | |
loadOptions.LoadFormat = LoadFormat.Mhtml; | |
// Instantiate Document class object to load the MTHML from MemoryStream | |
Aspose.Words.Document document = new Aspose.Words.Document(ms, loadOptions); | |
// Instantiate PdfSaveOptions class object | |
var saveOptions = new Aspose.Words.Saving.PdfSaveOptions(); | |
// Save the MHTML to PDF using C# | |
document.Save("SaveEmailAsPDF.pdf", saveOptions); | |
} | |
} | |
} | |
} |
在上面的示例中,为了将 EML 转换为 PDF,C#* 是在简单的 API 调用和明确定义的步骤的帮助下使用的。上述示例可用于 MSG 和 EML 格式的电子邮件文件。您可以通过设置 ColorMode、Compliance、ImageCompression 和 JpegQuality 等属性来自定义输出 PDF。
本文探讨了将 MSG 转换为基于 C#* 的 PDF 的 API。如果您正在寻找如何在 C# 中发送电子邮件,请参阅 如何在 C# 中发送电子邮件 上的文章。