Processing unread email attachment using Exchange web service CSharp:

In ASP.NET you would first need to instantiate Exchange Web Service (EWS) and then apply filter to process through folder and other filter criteria.
Here is sample code you can use :

public void ProcessUnReadEmailAttachments()

       {

try

           {

//Set all requred configuration parameters

this.Initialize(); //Click here for - how to create/initiate an instance of Exchange web service (EWS) in ASP.NET?

FindItemType findItemRequest = new FindItemType();

// Setup the mailbox using the smtp address of the account wanted

var mailbox = new EmailAddressType { EmailAddress = EmailAddress };

               findItemRequest.Traversal = ItemQueryTraversalType.Shallow;

// Define which item properties are returned in the response

ItemResponseShapeType itemProperties = new ItemResponseShapeType();

               itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;

               findItemRequest.ItemShape = itemProperties;  // Add properties shape to request

// Identify which folders to search to find items

//DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];

DistinguishedFolderIdType[] folderIDArray = new[] { new DistinguishedFolderIdType { Mailbox = mailbox } };

               folderIDArray[0] = new DistinguishedFolderIdType();

               folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;

               folderIDArray[0].Mailbox = mailbox;

// Add folders to request

               findItemRequest.ParentFolderIds = folderIDArray;

//Create unread only restriction --------------------------

RestrictionType restriction = new RestrictionType();

IsEqualToType isEqualTo = new IsEqualToType();

PathToUnindexedFieldType pathToFieldType = new PathToUnindexedFieldType();

               pathToFieldType.FieldURI = UnindexedFieldURIType.messageIsRead;

FieldURIOrConstantType constantType = new FieldURIOrConstantType();

ConstantValueType constantValueType = new ConstantValueType();

               constantValueType.Value = "0";

               constantType.Item = constantValueType;

               isEqualTo.Item = pathToFieldType;

               isEqualTo.FieldURIOrConstant = constantType;

               restriction.Item = isEqualTo;

               findItemRequest.Restriction = restriction;

this.HttpStatus(RequestOperation, findItemRequest, typeof(FindItemType));

// ------------- Send the request and get the response

FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);

this.HttpStatus(ResponseOperation, findItemResponse, typeof(FindItemResponseType));

// ------------- read returned

FindItemResponseMessageType folder = (FindItemResponseMessageType)findItemResponse.ResponseMessages.Items[0];

ArrayOfRealItemsType folderContents = new ArrayOfRealItemsType();

               folderContents = (ArrayOfRealItemsType)folder.RootFolder.Item;

ItemType[] items = folderContents.Items;

//string sText = "";

if (items != null)

               {

foreach (ItemType curItem in items)

                   {

//You can access email content here

//sText += "Subject: " + (curItem.Subject.Trim()) + "  ";

//sText += "DisplayTo: " + (curItem.DisplayTo.Trim()) + "  ";

//sText += "DateTimeReceived: " + (curItem.DateTimeReceived.TimeOfDay.ToString()) + " ";

//sText += "DateTimeReceived: " + (curItem.ItemClass.Trim()) + " ";

//sText += "\r\n";

                   }

               }

           }

catch (Exception x)

           {

//Write Exeption Handiling Code Here

string exceptonMessage = x.Message;

           }

       }

 

        /// <summary>

/// execte operation

/// </summary>

/// <param name="Operation">ServiceRequestOperation/ServiceResponceOperation</param>

/// <param name="serviceObject">executior object</param>

/// <param name="type">executior object type</param>

private void HttpStatus(string Operation, Object serviceObject, Type type)

       {

//// ------------- GetAccessibilityObjectById the stream

using (StreamWriter myreqWriter = new StreamWriter(Logger.LOG_PATH + Operation + ".xml"))

           {

XmlSerializer myReqSerializer = new XmlSerializer(type);

               myReqSerializer.Serialize(myreqWriter, serviceObject);

           }

       }