【问题标题】:How to download all attachments from email threads using EWS API 2.0 in C#如何在 C# 中使用 EWS API 2.0 从电子邮件线程下载所有附件
【发布时间】:2015-09-03 22:19:08
【问题描述】:

我已成功设置并从 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;

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

电子邮件场景:

这是带有新附件的第二个线程

附件2

2015 年 9 月 1 日星期二晚上 9:53,收藏 > 写道:

检查文档附件

附件一

【问题讨论】:

  • 那是因为附件只存在于那一封电子邮件中。对于发送和接收的消息,您也需要通过电子邮件对话历史记录。
  • 能否请您提供示例代码或任何有用的链接
  • 我现在手头没有任何东西,但这就是为什么您无法从以前的邮件中下载附件的原因。您将需要遍历每条消息,我将通过主题\对话为邮箱中的每个文件夹执行此操作,但由于您可能要通过的消息数量,它可能会很慢。跨度>

标签: c# exchangewebservices


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 2010-09-25
    • 1970-01-01
    • 2017-03-15
    • 2014-05-25
    相关资源
    最近更新 更多