【问题标题】:Download attachment from Exchange using Exchange Web Services使用 Exchange Web 服务从 Exchange 下载附件
【发布时间】:2011-08-24 21:07:23
【问题描述】:

我正在尝试使用以下代码连接并使用 C# 和 Exchange Web 服务从收件箱中的电子邮件下载附件,但我收到“System.ArgumentOutOfRangeException”错误,我不明白为什么。我已经用谷歌搜索了一个答案,但我找不到答案,或者我找到的答案是针对非常旧版本的 EWS。

我知道其余代码通常可以正常工作,因为我可以访问与电子邮件相关的其他信息,但不能访问附件。

谁能告诉我我的方法的错误?

提前致谢,

詹姆斯

    static void Main(string[] args)
    {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        service.Credentials = new NetworkCredential("MYLOGIN", "MYPASSWORD", "MYDOMAIN");

        service.Url = new Uri("https://MYMAILSERVER/EWS/Exchange.asmx");

        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(1000));

        foreach (Item item in findResults.Items)
        {
            if (item.HasAttachments && item.Attachments[0] is FileAttachment)
            {
                FileAttachment fileAttachment = item.Attachments[0] as FileAttachment;
                fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
            }

        }
    }
}

已解决但新问题

我现在通过将“foreach(findResults.Items 中的项目项)”更改为“foreach(findResults.Items 中的EmailMessage 项)”对问题进行了排序,但现在我需要了解如何枚举附件 - 任何有什么想法吗?

【问题讨论】:

  • 你从哪里得到异常?什么参数超出范围?该信息通常是异常的一部分。
  • 这是一个超出索引的错误。我现在通过将“foreach(findResults.Items 中的项目项)”更改为“foreach(findResults.Items 中的EmailMessage 项)”对问题进行了排序
  • 异常的“InnerException”部分可能也值得一看以获取此类信息

标签: c# exchangewebservices


【解决方案1】:

检查您的个人资料。如果您在轻量模式下运行,则附件不会随消息一起下载。

添加以下行

item.Load() // loads the entire message with attachment

【讨论】:

    【解决方案2】:

    你的新问题的答案是

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    
        //service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );
    
        service.AutodiscoverUrl("firstname.lastname@MyCompany.com");
    
            FindItemsResults<Item> findResults = service.FindItems(
               WellKnownFolderName.Inbox,
               new ItemView(10));
    
            foreach (Item item in findResults.Items)
            {
                Console.WriteLine(item.Subject);
                item.Load();
                if(item.HasAttachments)
                {
                    foreach (var i in item.Attachments)
                    {
                        FileAttachment fileAttachment = i as FileAttachment;
                        fileAttachment.Load();
                        Console.WriteLine("FileName: " + fileAttachment.Name);
    
                    }
                }
    
            }
    

    【讨论】:

      【解决方案3】:

      除非我遗漏了一些明显的东西,否则您只需通过item.Attachments 枚举即可。

      单击here 并向下滚动到您看到Example 标题的位置。

      【讨论】:

        【解决方案4】:

        从指定数量的电子邮件中下载所有附件的解决方案:

          ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
          service.Credentials = new NetworkCredential("login", "password");
        
          service.Url = new Uri("https://mail.Yourservername.com/EWS/Exchange.asmx");
        
          ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        
          FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
        
          if (findResults != null && findResults.Items != null && findResults.Items.Count > 0)
              foreach (EmailMessage item in findResults)
               {
                 EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments));
                   foreach (Attachment attachment in message.Attachment
                     {
                       if (attachment is FileAttachment)
                          {
                            FileAttachment fileAttachment = attachment as FileAttachment;      
                            fileAttachment.Load(@"Folder\file.name");
                           }
                     }
                }
        

        不要忘记将正确版本的 Exchange Server 传递给 ExchangeService 构造函数。

        【讨论】:

          【解决方案5】:

          这是一个 GetAttachmentsFromEmail 方法,可用于下载附件。

          public static void GetAttachmentsFromEmail(ExchangeService service, ItemId itemId)
              {
                  // Bind to an existing message item and retrieve the attachments collection.
                  // This method results in an GetItem call to EWS.
                  EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments));
          
                  // Iterate through the attachments collection and load each attachment.
                  foreach (Attachment attachment in message.Attachments)
                  {
                      if (attachment is FileAttachment)
                      {
                          FileAttachment fileAttachment = attachment as FileAttachment;
          
                          // Load the attachment into a file.
                          // This call results in a GetAttachment call to EWS.
                          fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
          
                          Console.WriteLine("File attachment name: " + fileAttachment.Name);
                      }
                      else // Attachment is an item attachment.
                      {
                          ItemAttachment itemAttachment = attachment as ItemAttachment;
          
                          // Load attachment into memory and write out the subject.
                          // This does not save the file like it does with a file attachment.
                          // This call results in a GetAttachment call to EWS.
                          itemAttachment.Load();
          
                          Console.WriteLine("Item attachment name: " + itemAttachment.Name);
                      }
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-03-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多