如何通过编程获取Outlook中所有未回复的邮件?
如何获取Outlook中所有未回复的邮件列表(编程实现)
在Outlook最新版本中,回复邮件后会在主题和邮箱地址下方显示“您已于……回复此邮件”的提示,同时有图标标识已回复。以下是获取所有未回复邮件列表的编程实现方案,针对你尝试代码中存在的问题给出修正:
你尝试的代码(存在问题)
private static void GetUnrepliedEmails() { Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application(); NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI"); MAPIFolder inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox); Items items = inboxFolder.Items; foreach (dynamic item in items) { if (item is MailItem) { // Get the value of the PR_LAST_VERB_EXECUTION_TIME property DateTime lastVerbExecutionTime = DateTime.MinValue; foreach (ItemProperty prop in item.ItemProperties) { if (prop.Name == "http://schemas.microsoft.com/mapi/proptag/0x10820040") { lastVerbExecutionTime = (DateTime)prop.Value; break; } } // Check if the item has been replied to if (lastVerbExecutionTime > DateTime.MinValue && item.ReceivedTime < lastVerbExecutionTime) { Console.WriteLine(item.Subject); Console.WriteLine("---- You have replied to the message."); } else { Console.WriteLine(item.Subject); Console.WriteLine("You have not replied to the message."); } } } }
修正后的解决方案
方法1:使用Restrict直接过滤未回复邮件
通过MAPI属性PR_LAST_VERB_EXECUTION_TIME(属性标签为0x10820040)筛选,未回复的邮件该属性值为NULL。使用Items.Restrict方法可以直接过滤出目标邮件,效率远高于逐个遍历:
using Microsoft.Office.Interop.Outlook; namespace TestOutlookCodes { class Program { static void Main(string[] args) { GetUnrepliedEmails(); } private static void GetUnrepliedEmails() { Application outlookApp = new Application(); NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI"); MAPIFolder inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox); // 筛选未回复的邮件:PR_LAST_VERB_EXECUTION_TIME属性为NULL string filter = "@SQL=\"http://schemas.microsoft.com/mapi/proptag/0x10820040\" IS NULL"; Items unrepliedItems = inboxFolder.Items.Restrict(filter); // 遍历筛选结果 foreach (var item in unrepliedItems) { if (item is MailItem mail) { Console.WriteLine($"未回复邮件主题:{mail.Subject}"); } } } } }
方法2:逐个检查邮件的回复状态(更精准)
如果需要区分“回复”“回复全部”“转发”等操作,可以同时检查PR_LAST_VERB_EXECUTED属性(标签0x10810003),该属性的数值对应不同操作:
- 102:已回复(Reply)
- 103:已回复全部(Reply All)
- 104:已转发(Forward)
通过判断这两个属性,可以精准识别是否已回复:
private static void GetUnrepliedEmails() { Application outlookApp = new Application(); NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI"); MAPIFolder inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox); foreach (var item in inboxFolder.Items) { if (item is MailItem mail) { bool hasBeenReplied = false; // 获取PR_LAST_VERB_EXECUTED属性值 int? lastVerb = mail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003") as int?; // 判断是否为回复操作 if (lastVerb.HasValue && (lastVerb == 102 || lastVerb == 103)) { hasBeenReplied = true; } if (!hasBeenReplied) { Console.WriteLine($"未回复邮件主题:{mail.Subject}"); } } } }
说明
- 避免遍历
ItemProperties集合,直接使用PropertyAccessor.GetProperty获取MAPI属性,效率更高。 - 仅通过时间判断可能误判转发邮件,结合
PR_LAST_VERB_EXECUTED属性可以精准识别回复操作。
内容的提问来源于stack exchange,提问作者10101
相关产品推荐
相关产品推荐

