首页 > 解决方案 > 使用win32com按Outlook中的对话线程对邮件进行分类/分组?

问题描述

有没有办法通过那里循环所有邮件,,,ConversationID并根据相同的id号对它们进行分组EntryID ,以便它们可以像outlooks功能一样打印出来。GetConversationConversationIndexfind related

outlook.GetItemFromID(id)仅适用于EntryID所有邮件独有的作品。

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

标签: pythonpython-3.xoutlookpywin32win32com

解决方案


您可以使用该类的GetConversation方法,该方法MailItem获取一个Conversation对象,该对象表示该项目所属的对话。

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

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

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

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); 
 } 
 } 
} 




推荐阅读