
- C# 库以处理 Outlook 便签
- 创建新便签
- 将新便签添加到 PST
- 从 PST 文件中检索 Outlook 便签
- 如何在 Exchange 服务器上创建新 Outlook 便签
- 从 Exchange 服务器获取便签
Outlook 便签为用户提供了一种方便的方式,在邮箱中创建快速、可访问的提醒。存储在 PST 文件中,这些提醒与电子邮件、联系人、日历事件和其他 Outlook 项目一起管理。对于以编程方式处理 Microsoft Outlook 数据的开发人员来说,访问、创建或管理便签是自动化工作流程或构建增强生产力的应用程序的关键。
在本文中,我们将探讨如何使用 .NET 库在 C# 中处理 Outlook 便签。本指南将涵盖如何加载和访问 PST 文件中的提醒、检索内容、创建新便签,甚至在 Exchange 服务器上管理它们。通过利用这些功能,开发人员可以以编程方式处理便签,促进集成、备份或自定义管理解决方案。
C# 库以处理 Outlook 便签
要在 C# 中创建和管理 Outlook 便签,我们将使用 Aspose.Email for .NET 库,该库提供强大的 API,用于处理各种与电子邮件和消息相关的功能,包括便签。该库支持在 PST 文件中添加、读取、更新和删除便签,非常适合处理 Outlook 数据。
安装
您可以通过 Visual Studio 中的 NuGet 包管理器或使用 NuGet 包管理器控制台安装 Aspose.Email for .NET。以下是两种方法:
- 在 Visual Studio 中使用 NuGet 包管理器:
- 在 Visual Studio 中打开您的项目。
- 转到 工具 > NuGet 包管理器 > 管理解决方案的 NuGet 包。
- 在浏览选项卡中搜索 Aspose.Email。
- 选择 Aspose.Email 包,然后单击 安装。
- 使用 NuGet 包管理器控制台:
- 通过导航到 工具 > NuGet 包管理器 > 包管理器控制台 打开 Visual Studio 中的 包管理器控制台。
- 运行以下命令以安装 Aspose.Email for .NET:
Install-Package Aspose.Email
或者,您可以直接从 Aspose 网站 下载 API 的最新版本。
安装完库后,您就可以开始在 C# 项目中处理 Outlook 便签。Aspose.Email API 使与 Outlook 数据的交互变得简单,确保您可以在 .NET 应用程序中高效处理它。
创建新便签
.NET 中的 MapiNote 类使您能够创建具有可自定义属性的新便签,例如颜色、主题和正文。配置完成后,便签可以保存为独立的 .msg
文件,以供在 Outlook 或其他兼容应用程序中进一步使用。
以下示例演示了如何创建新便签并将其保存到 .msg
文件中:
- 实例化一个 MapiNote 对象,并设置其属性,包括
Color
、Subject
和Body
,以定义便签的外观和内容。 - 使用 Save 方法,指定文件名和格式(
NoteSaveFormat.Msg
),将便签保存为独立的.msg
文件。
// Create a new MapiNote | |
var newNote = new MapiNote | |
{ | |
Color = NoteColor.Yellow, | |
Subject = "Meeting Reminder", | |
Body = "Remember to review the quarterly report before the meeting." | |
}; | |
// Save the note as an .msg file | |
newNote.Save("MeetingReminderNote.msg", NoteSaveFormat.Msg); |
通过这种方式,您可以轻松地创建和分发便签作为 .msg
文件,提供了一种灵活的方式在 PST 文件之外管理它们。
将新便签添加到 PST
要将新便签添加到 PST 文件,您可以创建一个 MapiNote 对象,并将其保存在 PST 的 “Notes” 文件夹中。该过程包括加载 PST 文件、访问适当的文件夹、创建提醒,然后将其添加到文件夹中。以下是逐步指南以及演示如何实现这一点的代码示例:
- 使用 PersonalStorage.FromFile 方法加载 PST 文件。
- 访问 PST 中的 “Notes” 文件夹。
- 创建一个新的 MapiNote 类实例,并设置所需的属性,例如颜色、主题和正文。
- 使用 AddMapiMessageItem 方法将便签添加到文件夹中。
// Load the PST file | |
using (var pst = PersonalStorage.FromFile(pstFilePath)); | |
// Access the Notes folder | |
var notesFolder = pst.RootFolder.GetSubFolder("Notes"); | |
if (notesFolder != null) | |
{ | |
// Create a new MapiNote | |
var newNote = new MapiNote | |
{ | |
Color = NoteColor.Yellow, // Set the desired note color | |
Subject = "Meeting Reminder", // Set the subject | |
Body = "Don't forget the meeting at 10 AM." // Set the note body | |
}; | |
// Add the sticky note to the Notes folder | |
notesFolder.AddMapiMessageItem(newNote); | |
} |
从 PST 文件中检索 Outlook 便签
要从 PST 文件中检索便签,您需要访问 Notes 文件夹并迭代其内容。以下示例演示如何加载 PST 文件、访问目标文件夹并获取其中的便签:
- 使用 PersonalStorage.FromFile 方法加载指定的 PST 文件。
- 使用 GetSubFolder(“Notes”) 从 PST 的根文件夹中检索 Notes 文件夹。
- 在文件夹上调用 EnumerateMapiMessages 方法并返回所有 MAPI 消息。代码过滤这些消息以仅包含
MapiNote
对象。 - 显示每个获取的便签的细节,包括其主题、颜色、高度、宽度和正文,并将其打印到控制台。
// Load the PST file | |
using (var pst = PersonalStorage.FromFile("YourPSTFile.pst")); | |
// Access the Notes folder | |
var notesFolder = pst.RootFolder.GetSubFolder("Notes"); | |
if (notesFolder != null) | |
{ | |
// Iterate through items in the Notes folder | |
foreach (var note in notesFolder.EnumerateMapiMessages().OfType<MapiNote>()) | |
{ | |
// Process the sticky note details | |
Console.WriteLine("Note Subject: " + note.Subject); | |
Console.WriteLine("Note Color: " + note.Color); | |
Console.WriteLine("Note Height: " + note.Height); | |
Console.WriteLine("Note Width: " + note.Width); | |
Console.WriteLine("Note Body: " + note.Body); | |
Console.WriteLine(); | |
} | |
} |
如何在 Exchange 服务器上创建新 Outlook 便签
要在 Exchange 服务器上创建新便签,您可以利用 EWSClient 与服务器的邮箱进行交互。该过程涉及建立与 Exchange 服务器的连接、创建新的 MapiNote
对象,然后将其保存到 Notes 文件夹中。以下是逐步指南和代码示例:
- 首先,您需要使用 EWSClient 类建立与 Exchange 服务器的连接。这需要服务器 URL 和适当的身份验证凭据。
- 创建一个新的 MapiNote 实例,并设置其属性,例如颜色、主题和正文。
- 使用 EWSClient 的 CreateItem 方法将新便签保存到 Exchange 邮箱的指定文件夹中。
以下是演示如何在 Exchange 服务器上创建和保存新便签的代码示例:
// Replace these with your actual credentials and server information | |
string userName = "your_email@example.com"; | |
string accessToken = "your_access_token"; // OAuth access token | |
// Create new note instance | |
var newNote = new MapiNote | |
{ | |
Color = NoteColor.Yellow, | |
Subject = "Meeting Reminder", | |
Body = "Don't forget about the meeting tomorrow at 10 AM." | |
}; | |
// Set up the EWS client | |
using var client = EWSClient.GetEWSClient("https://outlook.office365.com/EWS/Exchange.asmx", | |
new OAuthNetworkCredential(userName, accessToken)); | |
// Create the note on the Exchange Server | |
client.CreateItem(client.MailboxInfo.NotesUri, newNote); |
通过遵循此过程,您可以直接在 Exchange 服务器上创建便签。
从 Exchange 服务器获取便签
该过程涉及对 Exchange 服务器进行身份验证、列出可用的便签,然后获取每个便签的详细信息。以下是您可以完成此操作的方法:
- 首先,您需要使用您的 Exchange 服务器凭据创建 EWSClient 的实例。
- 使用 ListMessages 方法获取 Notes 文件夹中的消息列表。
- 对于每个便签,使用 FetchMapiNotes 方法检索其详细信息。
以下是说明这些步骤的代码示例:
// Define the credentials and EWS endpoint | |
string userName = "your-email@example.com"; | |
string accessToken = "your-access-token"; | |
// Create an instance of the EWS client | |
using var client = EWSClient.GetEWSClient("https://outlook.office365.com/EWS/Exchange.asmx", new OAuthNetworkCredential(userName, accessToken)); | |
// List all notes in the Notes folder | |
var messageInfos = client.ListMessages(client.MailboxInfo.NotesUri); | |
// Fetch the MAPI notes using the unique URIs of the listed messages | |
var notes = client.FetchMapiNotes(messageInfos.Select(info => info.UniqueUri)); | |
// Process and display each fetched note | |
foreach (var note in notes) | |
{ | |
Console.WriteLine("Note Subject: " + note.Subject); | |
Console.WriteLine("Note Color: " + note.Color); | |
Console.WriteLine("Note Body: " + note.Body); | |
} |
将 your-email@example.com
和 your-access-token
替换为您的实际 Exchange 电子邮件和 OAuth 访问令牌。
这种方法使您能够高效管理和访问存储在 Exchange 服务器上的便签。
结论
使用 C# .NET 管理 Outlook 便签提供了一种在工作区中组织和检索重要信息的方法。通过利用 MAPI 协议的能力,开发人员可以创建、读取和更新便签,无论是存储在本地 PST 文件中还是在 Exchange 服务器上。
通过概述的方法,用户可以通过编程管理他们的数据,从而增强生产力,更好地与其他应用程序和工作流程集成。无论是为快速提醒创建新便签,还是获取现有便签以供参考,提供的工具都促进了有效的信息管理。
免费资源
除了其进阶的 C# 电子邮件 API 外,该库还提供了许多免费资源:
评估 Aspose.Email
您可以通过其 评估版本 免费尝试 Aspose.Email 的所有功能。
获取临时许可证
如果您想在没有评估版本限制的情况下测试 Aspose.Email,您还可以申请 30 天的 临时许可证。免费试用将让您对服务的能力有一个良好的了解,并让您进行一些早期开发。