none
Get the textual body of a sent email, as well as the textual body of the email that is being replied to. RRS feed

  • Question

  • Hello!

    I'm interested in emails which are replies to previous emails. I want to get the text of this kind of email, and the "stimulus" text that the reply is to (the previous email in the conversation).

    I can fairly easily load the user's Sent emails, which correspond to sent emails. 

    I'm new to the managed API, and I'm aware of something called "conversations" which might help.

    Otherwise, I'm happy to try to split the raw textual email using heuristics. What is the best way to get the raw email?

    I'm also not sure how MIME fits into this!

    Many thanks!


    • Edited by Sumo10 Tuesday, September 16, 2014 3:09 PM
    Tuesday, September 16, 2014 3:08 PM

All replies

  • Conversations would be a good approach, as you can use the new GetConversationItems method (new in Exchange 2013) to grab all the messages in the conversation. Here's a sample that finds the first item in your inbox, then gets all items in the conversation, but ignores items in the Deleted Items and the Drafts folder (which is configurable).

     static void GetConversationItemsSingleConversation(ExchangeService service)
            {
                try
                {
                    // Find the first X items in the inbox.
                    FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox,
                                                                       new ItemView(1));
    
                    // Get the conversation identifiers for the items. 
                    ConversationId convId = results.Items[0].ConversationId;
    
                    // Specify the properties that will be 
                    // returned for the items in the conversation.
                    PropertySet properties = new PropertySet(BasePropertySet.IdOnly,
                                                              ItemSchema.Subject,
                                                              ItemSchema.DateTimeReceived, EmailMessageSchema.TextBody);
    
                    // Identify the folders to ignore.
                    Collection<FolderId> foldersToIgnore = new Collection<FolderId>() { WellKnownFolderName.DeletedItems, WellKnownFolderName.Drafts };
    
                    // Request conversation items. This results in a call to the service.         
                    ConversationResponse response = service.GetConversationItems(convId,
                                                                                   properties,
                                                                                   null,
                                                                                   foldersToIgnore,
                                                                                   ConversationSortOrder.TreeOrderDescending);
    
                    // Get the synchronization state of the conversation.
                    Console.WriteLine("SyncState: " + response.SyncState);
    
                    Collection<Item> items = new Collection<Item>();
    
                    // Process each node of conversation items.
                    foreach (ConversationNode node in response.ConversationNodes)
                    {
                        Console.WriteLine("Conversation node items:");
    
                        // Process each item in the conversation node.
                        foreach (Item item in node.Items)
                        {
                            Console.WriteLine("   Item ID: " + item.Id.UniqueId);
                            Console.WriteLine("   Subject: " + item.Subject);
                            Console.WriteLine("   Received: " + item.DateTimeReceived);
                            Console.WriteLine("   Text body: " + item.TextBody);
                            Console.WriteLine("");
                            items.Add(item);
                        }
                    }
                }
                // This exception may occur if there is an error with the service.
                catch (ServiceResponseException srException)
                {
                    Console.WriteLine(srException);
                }
            }

    Additional info about conversations can be found here: How to: Work with conversations by using EWS in Exchange.

    Tuesday, September 16, 2014 6:56 PM
    Moderator