In previous article (How to process unread email attachment using Exchange web service in ASP.NET?) we saw how to iterate through unread emails and process data, now here we are going to see how to iterate through the attachments of  email.

 

for that we are going to use following code block where we are going to pass instance of Exchange Service binding and item ID of email.

 

 

       /// <summary>

 

       /// Get all items associated with email

       /// </summary>

       /// <param name="binding"></param>

       /// <param name="itemID"></param>

       public void GetAttachmentsByItemID(ExchangeServiceBinding binding, string itemID)

       {

           try

           {

               GetItemType getItemRequest = new GetItemType();

               ItemIdType[] items = new ItemIdType[1];

               items[0] = new ItemIdType();

               items[0].Id = itemID;

               getItemRequest.ItemIds = items;

               getItemRequest.ItemShape = new ItemResponseShapeType();

               getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;

               PathToUnindexedFieldType hasAttachPath = new PathToUnindexedFieldType();

               hasAttachPath.FieldURI = UnindexedFieldURIType.itemHasAttachments;

               PathToUnindexedFieldType attachmentsPath = new PathToUnindexedFieldType();

               attachmentsPath.FieldURI = UnindexedFieldURIType.itemAttachments;

               // Add additional properties?

               getItemRequest.ItemShape.AdditionalProperties = new BasePathToElementType[]{

            hasAttachPath, attachmentsPath };

               GetItemResponseType getItemResponse = binding.GetItem(getItemRequest);

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

 

               if (getItemResponseMessage.ResponseCode == ResponseCodeType.NoError)

 

               {

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

                   MessageType messageType = item as MessageType;

                   SingleRecipientType singleRecipientType = messageType.From;

                   

                   // Don't rely on  HasAttachments - It does not mean what you thing it would.

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

                   {

                       FileAttachmentType almostAnAttachment;

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

                       {

                           almostAnAttachment = item.Attachments[attachmentIndex] as FileAttachmentType;

//You can add filter if you are looking for specific type of attachement. In this example I am filtering only .xml files.

                           if (almostAnAttachment != null && string.Equals(almostAnAttachment.Name.ToLower().Substring(almostAnAttachment.Name.ToLower().Length - 4, 4), ".xml"))

                           {

                               //ProcessAttachmentByAttachmentItemID(binding, ((RequestAttachmentIdType)(almostAnAttachment.AttachmentId)).Id);

                               //SetReadFlagForMessage(item.ItemId);

                           }

                       }

                   }

               }

           }

           catch (Exception x)

           {

               //Write Exeption Handiling Code Here

               string exceptonMessage = x.Message;

           }

       }