gpt4 book ai didi

c# - 在 Outlook 中检索当前电子邮件正文

转载 作者:太空狗 更新时间:2023-10-29 20:03:20 25 4
gpt4 key购买 nike

在我的 outlook 插件中,我想在功能区上添加一个按钮,所以当用户单击此按钮时,我想检索当前选择的电子邮件的正文,我有这段代码,但它只从收件箱中检索第一封电子邮件,因为索引是1:

Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
String body = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Body;

那么如何在outlook中找回当前打开的邮件呢? ,此方法对我有用,但我需要获取当前电子邮件的索引。

谢谢。

最佳答案

您不应该每次都初始化一个新的 Outlook.Application() 实例。大多数加载项框架为您提供了一个 Outlook.Application 实例,对应于当前的 Outlook session ,通常通过名为 Application 的字段或属性。您应该在加载项的整个生命周期内使用它。

要获取当前选定的项目,请使用:

Outlook.Explorer explorer = this.Application.ActiveExplorer();
Outlook.Selection selection = explorer.Selection;

if (selection.Count > 0) // Check that selection is not empty.
{
object selectedItem = selection[1]; // Index is one-based.
Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;

if (mailItem != null) // Check that selected item is a message.
{
// Process mail item here.
}
}

请注意,上面的代码会让您处理第一个 选定的项目。如果您选择了多个项目,您可能希望循环处理它们。

关于c# - 在 Outlook 中检索当前电子邮件正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10935611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com