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

时间:2019-07-09 09:09:12

标签: python python-3.x outlook pywin32 win32com

是否有一种方法可以遍历ConversationIDEntryID那里的所有邮件 ,GetConversationConversationIndex并根据相同的ID号对其进行分组,以便可以将它们与Outlook find related功能相同地打印出来。

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

我试图遍历所有邮件,并在其中找到各自的ID号,如果有任何具有相同ID号的邮件,则进行匹配。 但是所有id似乎都是唯一的。

1 个答案:

答案 0 :(得分:2)

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

如果该项目不存在对话,则

GetConversation返回Null(在Visual Basic中为Nothing)。在以下情况下,该项目没有对话:

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

下面的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); 
 } 
 } 
}