In previous article (How to iterate through the attachments using Exchange web service in ASP.NET?) we saw how to iterate through attachments contained in unread email and filtering based on type. I applied filter to process only attachment of xml type.

 

 


Here will see how we can process xml file in outlook email.

      

       /// <summary>

       /// Here is code to process attachement

       /// </summary>

       /// <param name="esb"></param>

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

       private static void ProcessAttachmentByAttachmentItemID(ExchangeServiceBinding esb, string itemID)

       {

           // Create the request.

           GetAttachmentType request = new GetAttachmentType();

 

           // Create the response shape.

 

           AttachmentResponseShapeType responseShape = new AttachmentResponseShapeType();

           responseShape.BodyType = BodyTypeResponseType.Text;

           responseShape.BodyTypeSpecified = true;

 

           // Add the response shape to the request.

 

           request.AttachmentShape = responseShape;

 

           // Identify the attachment IDs to get.

 

           RequestAttachmentIdType[] ids = new RequestAttachmentIdType[1];

           ids[0] = new RequestAttachmentIdType();

           ids[0].Id = itemID;

 

           // Add the attachment IDs to the request.

 

           request.AttachmentIds = ids;

 

           GetAttachmentResponseType response = esb.GetAttachment(request);

 

           ResponseMessageType[] rmta = response.ResponseMessages.Items;

 

           foreach (ResponseMessageType responseMessage in rmta)

 

           {

               AttachmentInfoResponseMessageType airmt = (responseMessage as AttachmentInfoResponseMessageType);

               AttachmentType[] attachments = airmt.Attachments;

 

               // Type check for item or file attachment.

 

               foreach (AttachmentType attachment in attachments)

               {

                   if (attachment is FileAttachmentType)

                   {

                       FileAttachmentType fat = (attachment as FileAttachmentType);

 

                       // Assumes ASCII encoding.

 

                       //string myContent = ASCIIEncoding.ASCII.GetString(fat.Content);

                       string myContent = ASCIIEncoding.UTF8.GetString(fat.Content);

                       //my Content is attachment content - you can download/write code here to save it into database

                       //SaveAttachmentContentInDatabase(myContent);

                   }

 

                   // Attachment is item attachment.

 

                   else

                   {

                       ItemAttachmentType iat = (attachment as ItemAttachmentType);

 

                       // TODO: Handle the item attachment.

 

                   }

               }

           }

       }