简体   繁体   中英

C# Get last reply in Outlook conversation

How to get the last reply in an Outlook conversation? For example, the following code:

Outlook.MailItem item = Items.GetLast();
MessageBox.Show(item.Body);

will display the entire email, not only the last reply.

The following example shows how to get and display mail items in a conversation.

void DemoConversation()
{
   object selectedItem = Application.ActiveExplorer().Selection[1];
   // For this example, you will work only with 
   //MailItem. Other item types such as
   //MeetingItem and PostItem can participate 
   //in Conversation.
   if (selectedItem is Outlook.MailItem)
   {
      // Cast selectedItem to MailItem.
      Outlook.MailItem mailItem = selectedItem as Outlook.MailItem; 
      // Determine store of mailItem.
      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 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 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 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 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);
      }
   }
}

In the sample code example, we get a selected MailItem object and then determine the store of the MailItem object by using the Store property of the Folder object. DemoConversation then checks whether the IsConversationEnabled property is true; if it is true, the code example gets Conversation object by using the GetConversation method. If the Conversation object is not a null reference, the example gets the associated Table object that contains each item in the conversation by using the GetTable method. The example then enumerates each item in the Table and calls EnumerateConversation on each item to access the child nodes of each item. EnumerateConversation takes a Conversation object and gets the child nodes by using the GetChildren(Object) method. EnumerateConversation is called recursively until there are no more child nodes. Each conversation item is then displayed to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM