下载附件 [英] Download Attachments

查看:80
本文介绍了下载附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用MS Exchange Web服务API,并且能够连接到特定的电子邮件帐户。

Hello, I am using MS Exchange Web Services API and am able to connect to a specific email account.

我想下载未读电子邮件的所有附件。如果你能帮我解决这个问题,我将不胜感激。

I want to download all the attachments of the unread emails. I would appreciate if you can help me out with this one.

以下是

Below is the code and problem with the

GetAttachmentsOnItem方法是它需要ItemTypeID而我不知道如何从item获取ItemTypeID。

GetAttachmentsOnItem method is that it expects ItemTypeID and I don;t know how to get the ItemTypeID from item.

请帮助

 

      ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
      service.AutodiscoverUrl("genemail@mycompany.com");


      

      ExchangeServiceBinding esb = new ExchangeServiceBinding();
      esb.Url = "https://mt/EWS/exchange.asmx";
      esb.Credentials = new NetworkCredential("genemail", "ABCD@!!!", "custDomain");


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

      string attachments=@"c:\Test\";

      foreach (Item item in findResults1.Items)
      {
        if (item.HasAttachments)
        {

          //GetAttachmentsOnItem(esb, item.Id, attachments);
          
          MessageBox.Show(item.Subject);          
        }
      }





 static void GetAttachmentsOnItem(ExchangeServiceBinding binding, ItemIdType id, string destinationPath)
    {

      // STEP 1: We need to call GetItem on the Id so that we can get the Attachments collection back

      GetItemType getItemRequest = new GetItemType();

      getItemRequest.ItemIds = new ItemIdType[] { id };

      getItemRequest.ItemShape = new ItemResponseShapeType();


      // For this example, all we really need is the HasAttachments and the Attachment Collection.

      // As such, let's just request those props to reduce network traffic.

      //

      getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.IdOnly;

      PathToUnindexedFieldType hasAttachPath = new PathToUnindexedFieldType();

      hasAttachPath.FieldURI = UnindexedFieldURIType.itemHasAttachments;

      PathToUnindexedFieldType attachmentsPath = new PathToUnindexedFieldType();

      attachmentsPath.FieldURI = UnindexedFieldURIType.itemAttachments;

      // Add these to our additional properties...

      //

      getItemRequest.ItemShape.AdditionalProperties = new BasePathToElementType[]{

         hasAttachPath, attachmentsPath };

      // Now make the call

      // 

      GetItemResponseType getItemResponse = binding.GetItem(getItemRequest);

      // getItem returns ItemInfoResponseMessages. Since we only requested one item, we should only 

      // get back one response message.

      ItemInfoResponseMessageType getItemResponseMessage = getItemResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;


      // Like all good, happy and compliant developers, we should check our response code...

      //

      if (getItemResponseMessage.ResponseCode == ResponseCodeType.NoError)
      {

        // STEP 2: Grab the Attachment Ids from our item

        ItemType item = getItemResponseMessage.Items.Items[0];

        if (item.HasAttachments && (item.Attachments != null) && (item.Attachments.Length > 0))
        {

          List<RequestAttachmentIdType> attachmentIds = new List<RequestAttachmentIdType>();

          for (int attachmentIndex = 0; attachmentIndex < item.Attachments.Length; attachmentIndex++)
          {

            // For now, let's only consider file attachments instead of item attachments.

            //

            FileAttachmentType almostAnAttachment = item.Attachments[attachmentIndex] as FileAttachmentType;

            if (almostAnAttachment != null)
            {

              // VERY IMPORTANT! The attachment collection returned by GetItem only has meta data

              // about the attachments, but DOES NOT INCLUDE THE ACTUAL CONTENT. We must use

              // GetAttachment to get the actual attachment.

              //

              RequestAttachmentIdType requestId = new RequestAttachmentIdType();

              requestId.Id = almostAnAttachment.AttachmentId.Id;

              attachmentIds.Add(requestId);

            }

          }

          // now that we have all of the attachment ids, let's make a single GetAttachment call to 

          // get them all in one shot.

          //

          GetAttachmentType getAttachmentRequest = new GetAttachmentType();


          // Oddly enough, just create an EMPTY (non-null) attachment response shape.

          //

          getAttachmentRequest.AttachmentShape = new AttachmentResponseShapeType();

          getAttachmentRequest.AttachmentIds = attachmentIds.ToArray();

          GetAttachmentResponseType getAttachmentResponse = binding.GetAttachment(getAttachmentRequest);


          // Now, here we asked for multiple items. As such, we will get back multiple response

          // messages.

          foreach (AttachmentInfoResponseMessageType attachmentResponseMessage in getAttachmentResponse.ResponseMessages.Items)
          {

            if (attachmentResponseMessage.ResponseCode == ResponseCodeType.NoError)
            {

              // We only asked for file attachments above, so we should only get FileAttachments.

              // If you are really paranoid, you can check for null after this again.

              //

              FileAttachmentType fileAttachment = attachmentResponseMessage.Attachments[0] as FileAttachmentType;

              // Now, just save out the file contents

              //

              using (FileStream file = File.Create(

                    Path.Combine(destinationPath, fileAttachment.Name)))
              {

                file.Write(fileAttachment.Content, 0, fileAttachment.Content.Length);

                file.Flush();

                file.Close();

              }

            }

          }

        }

      }

    }

 

推荐答案

你为什么同时使用两者EWS托管API(ExchangeService)和自动生成的代理(ExchangeServiceBinding)?只需摆脱自动生成的代理(删除您的网站或服务参考),让您的生活更轻松。

Why are you using both the EWS Managed API (ExchangeService) and auto-generated proxies (ExchangeServiceBinding)? Just get rid of auto-generated proxies (remove your web or service reference) and make your life easier.

回答您的具体问题:

Item item = Item.Bind(service, id, PropertySet.FirstClassProperties);

// Load all the file attachments to disk
foreach (FileAttachment attachment in item.Attachments)
{
  if (attachment != null)
  {
    attachment.Load(<path to file>);
  }
}


service是一个ExchangeService实例,id是从FindItems调用中获得的ItemId实例。

service is an ExchangeService instance, id is an ItemId instance that you get from the FindItems call.


这篇关于下载附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆