使用 Exchange Web 服务从 Exchange 下载附件

本文关键字:Exchange 下载 服务 Web 使用 | 更新日期: 2023-09-27 17:56:33

我正在尝试使用以下代码使用 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中的电子邮件项目)"来解决问题,但现在我需要了解如何通过附件枚举 - 有人有什么想法吗?

使用 Exchange Web 服务从 Exchange 下载附件

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

添加以下行

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

新问题的答案是

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

除非我错过了一些明显的东西,否则您需要做的就是通过item.Attachments枚举。

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

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

  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构造函数。

这是一种方法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);
            }
        }
    }