簡體   English   中英

使用Exchange Web服務從Exchange下載附件

[英]Download attachment from Exchange using Exchange Web Services

我試圖使用以下代碼使用C#和Exchange Web服務從收件箱中的電子郵件連接和下載附件,但我收到'System.ArgumentOutOfRangeException'錯誤,我不明白為什么。 我已經google了一個答案,但我找不到一個或我找到的答案是非常舊版本的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項)'來排序問題,但現在我需要找出如何通過附件進行枚舉 - 任何人的想法?

檢查你的個人資料 如果您在輕型模式下運行,則不會使用消息下載附件。

添加以下行

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

                }
            }

        }

從指定數量的電子郵件下載所有附件的解決方案:

  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構造函數。

除非我遺漏了一些顯而易見的東西,否則你需要做的就是通過item.Attachments枚舉。

單擊此處並向下滾動到您看到Example標題的位置。

這是一種可用於下載附件的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);
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM