使用 win32com 在 outlook 中按会话线程对邮件进行分类/分组?

Classify / Group mails by conversation thread in outlook using win32com?

有没有办法通过那里循环遍历所有邮件ConversationIDEntryID , GetConversation, ConversationIndex 并根据相同的 id 编号将它们分组,以便它们可以像 outlooks find related 函数一样打印出来。

作为 outlook.GetItemFromID(id) 仅适用于 EntryID,它对所有邮件都是唯一的。

我试图遍历所有邮件并找到各自的 ID 号并匹配是否有具有相同 ID 号的邮件。 但是所有的 id 似乎都是独一无二的。

您可以使用代表该项目所属对话的 GetConversation method of the MailItem class which obtains a Conversation 对象。

GetConversation returns Null(在 Visual Basic 中为 Nothing)如果该项目不存在对话。在以下情况下,项目不存在对话:

  • 该项目尚未保存。可以通过用户操作或 auto-save.
  • 以编程方式保存项目
  • 对于可以发送的项目(例如,邮件项目、约会项目或联系人项目),该项目尚未发送。
  • 已通过 Windows 注册表禁用对话。
  • 该商店不支持 Conversation 视图(例如,Outlook 运行 在经典联机模式下针对早于 Microsoft Exchange Server 2010 的 Microsoft Exchange 版本)。使用 Store 对象的 IsConversationEnabled 属性 来确定商店是否支持对话视图。

以下 C# 示例代码(抱歉,我不熟悉 python,但 Outlook 对象模型对所有编程语言都是通用的)假定资源管理器中的选定项目 window是邮件项目。该代码示例获取与所选邮件项目关联的对话,并枚举该对话中的每个项目,显示项目的主题。

void DemoConversation() 
{ 
 object selectedItem = 
 Application.ActiveExplorer().Selection[1]; 
 // This example uses only 
 // MailItem. Other item types such as 
 // MeetingItem and PostItem can participate 
 // in the conversation. 
 if (selectedItem is Outlook.MailItem) 
 { 
 // Cast selectedItem to MailItem. 
 Outlook.MailItem mailItem = 
 selectedItem as Outlook.MailItem; 
 // Determine the store of the mail item. 
 Outlook.Folder folder = mailItem.Parent 
 as Outlook.Folder; 
 Outlook.Store store = folder.Store; 
 if (store.IsConversationEnabled == true) 
 { 
 // Obtain a Conversation object. 
 Outlook.Conversation conv = 
 mailItem.GetConversation(); 
 // Check for null Conversation. 
 if (conv != null) 
 { 
 // Obtain Table that contains rows 
 // for each item in the conversation. 
 Outlook.Table table = conv.GetTable(); 
 Debug.WriteLine("Conversation Items Count: " + 
 table.GetRowCount().ToString()); 
 Debug.WriteLine("Conversation Items from Table:"); 
 while (!table.EndOfTable) 
 { 
 Outlook.Row nextRow = table.GetNextRow(); 
 Debug.WriteLine(nextRow["Subject"] 
 + " Modified: " 
 + nextRow["LastModificationTime"]); 
 } 
 Debug.WriteLine("Conversation Items from Root:"); 
 // Obtain root items and enumerate the conversation. 
 Outlook.SimpleItems simpleItems 
 = conv.GetRootItems(); 
 foreach (object item in simpleItems) 
 { 
 // In this example, enumerate only MailItem type. 
 // Other types such as PostItem or MeetingItem 
 // can appear in the conversation. 
 if (item is Outlook.MailItem) 
 { 
 Outlook.MailItem mail = item 
 as Outlook.MailItem; 
 Outlook.Folder inFolder = 
 mail.Parent as Outlook.Folder; 
 string msg = mail.Subject 
 + " in folder " + inFolder.Name; 
 Debug.WriteLine(msg); 
 } 
 // Call EnumerateConversation 
 // to access child nodes of root items. 
 EnumerateConversation(item, conv); 
 } 
 } 
 } 
 } 
} 


void EnumerateConversation(object item, 
 Outlook.Conversation conversation) 
{ 
 Outlook.SimpleItems items = 
 conversation.GetChildren(item); 
 if (items.Count > 0) 
 { 
 foreach (object myItem in items) 
 { 
 // In this example, enumerate only MailItem type. 
 // Other types such as PostItem or MeetingItem 
 // can appear in the conversation. 
 if (myItem is Outlook.MailItem) 
 { 
 Outlook.MailItem mailItem = 
 myItem as Outlook.MailItem; 
 Outlook.Folder inFolder = 
 mailItem.Parent as Outlook.Folder; 
 string msg = mailItem.Subject 
 + " in folder " + inFolder.Name; 
 Debug.WriteLine(msg); 
 } 
 // Continue recursion. 
 EnumerateConversation(myItem, conversation); 
 } 
 } 
}