Prateek Regmi
Prateek Regmi

Reputation: 53

How to download all attachments from email threads using EWS API 2.0 in C#

I have setup and downloaded attachments from EWS successfully and here is the brief code i am working on:

 EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments));

                           foreach (Attachment attachment in message.Attachments)
                           {
                               if (attachment is FileAttachment)
                               {
                                   string sFilePath;
                                   FileAttachment fileAttachment = attachment as FileAttachment;

I came across the problem where it is only downloading the attachment from the latest email thread not from the previous threads. Look at below email scenario I sent a email with attachment1 to my friend, he replied with Attachment2 to me.. how can i retrieve both the attachments from the email and relate to the email threads they belong to.

Email Scenario:

This is Second thread with new attachment

Attachment 2

On Tue, Sep 1, 2015 at 9:53 PM, stash it > wrote:

Checking Document Attachment

Attachment 1

Upvotes: 0

Views: 818

Answers (1)

Prateek Regmi
Prateek Regmi

Reputation: 53

I Figured it out:

ConversationId convId = item.ConversationId; 

PropertySet properties = new PropertySet(BasePropertySet.IdOnly,                                                       
                                                ItemSchema.Subject,
                                                ItemSchema.InReplyTo,
                                                ItemSchema.DateTimeReceived,
                                                ItemSchema.DateTimeSent,
                                                ItemSchema.DisplayCc,
                                                ItemSchema.IsFromMe,                                                         
                                                ItemSchema.DisplayTo,
                                                ItemSchema.HasAttachments,
                                                ItemSchema.Attachments,
                                                ItemSchema.UniqueBody);

// Request conversation items. This results in a call to the service.         
ConversationResponse response = service.GetConversationItems
(convId,properties,null,null,
ConversationSortOrder.TreeOrderDescending);

foreach (ConversationNode node in response.ConversationNodes)
{
    foreach (Item item in node.Items)                   
         {
        Console.WriteLine("   Received: " + item.DateTimeReceived);
        Console.WriteLine("   Received: " + item.uniquebody);
        Console.WriteLine("   Received: " + item.subject);
         if (item.HasAttachments)
                 {
              foreach(Attachment attach in item.Attachments)
                           {
                FileAttachment fileAttachment = attach as FileAttachment;
                fileAttachment.Load(sFilePath);
                }
          }
      }
}

Reference: https://msdn.microsoft.com/en-us/library/office/dn610351(v=exchg.150).aspx

Upvotes: 2

Related Questions