如何使用C#中的EWS API 2.0从电子邮件线程下载所有附件

时间:2015-09-01 18:15:30

标签: c# exchangewebservices

我已经成功设置并下载了EWS的附件,这是我正在处理的简要代码:

 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;

我遇到的问题是它只从最新的电子邮件线程下载附件,而不是从以前的线程下载。看下面的电子邮件场景我发送了一封带有attachment1的电子邮件给我的朋友,他用Attachment2回复了我..如何从电子邮件中检索这两个附件并与他们所属的电子邮件线程相关。

电子邮件场景:

  

这是带有新附件的第二个帖子

     

附件2

     

2015年9月1日星期二晚上9:53,将它藏起来   >写道:

     

检查文件附件

     

附件1

1 个答案:

答案 0 :(得分:1)

我想出来了:

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

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