Figdor
Figdor

Reputation: 45

How to track mail correspondances

Does anybody know if it is possible to find out if an email in my Inbox is a repsonse to a mail I have sent earlier?

Example: I send an email to person A with a question. When person A responds I would like to link my original mail to his response. My aim is to save the entire mail correspondance in a custom folder.

I have tried adding a MailItem.UserProperty to the E-mail object but apparently it doesn't get send.

An alternative solution I have thought about is to add an invisble text within the mail body that I can search when the response is recieved (assuming my original mail will be included in the response).

Upvotes: 1

Views: 1888

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49405

The MailItem class provides the following properties for tracking conversations:

  • ConversationID - a string that uniquely identifies a Conversation object that the MailItem object belongs to.
  • ConversationTopic - a string representing the topic of the conversation thread of the Outlook item. It is the normalized subject of the message, the subject without the prefix strings.
  • ConversationIndex - a string that indicates the relative position of the item within the conversation thread.

As you may see the conversation grouping depends on the subject line.

You can use the GetConversation method of the MailItem class to get a Conversation object that represents the conversation to which this item belongs. Be aware, GetConversation returns Null (Nothing in Visual Basic) if no conversation exists for the item. No conversation exists for an item in the following scenarios:

  • The item has not been saved. An item can be saved programmatically, by user action, or by auto-save.
  • For an item that can be sent (for example, a mail item, appointment item, or contact item), the item has not been sent.
  • Conversations have been disabled through the Windows registry.
  • The store does not support Conversation view (for example, Outlook is running in classic online mode against a version of Microsoft Exchange earlier than Microsoft Exchange Server 2010). Use the IsConversationEnabled property of the Store object to determine whether the store supports Conversation view.

For example:

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

Upvotes: 2

Related Questions