Changing status of emails from ‘unread’ to ‘read’ using Exchange web service (EWS)-

In previous article (How to process email attachments using Exchange web service in ASP.NET?) we saw how to iterate through attachment  and process them. Once it is processed you would like to mark as read (change status from ‘unread’ to ‘read’ )

 

 

Here will se how we can do that.

 

              

       /// <summary>

       /// EWS code to update the IsRead flag, *ONLY* for MessageType

       /// </summary>

       /// <param name="messageId">email massage/item id</param>

       /// <returns>boolean value true- if it marked as read else false</returns>

       private static bool SetReadFlagForMessage(ItemIdType messageId)

       {

           SetItemFieldType setField = new SetItemFieldType();

           MessageType message = new MessageType();

 

           // message.IsRead = false;

 

           message.IsRead = true;

           message.IsReadSpecified = true;

           setField.Item1 = message;

 

           PathToUnindexedFieldType path = new PathToUnindexedFieldType();

 

           path.FieldURI = UnindexedFieldURIType.messageIsRead;

           setField.Item = path;

 

           ItemChangeType[] updatedItems = new ItemChangeType[1];

 

           updatedItems[0] = new ItemChangeType();

           updatedItems[0].Updates = new ItemChangeDescriptionType[1];

           updatedItems[0].Updates[0] = setField;

 

           ItemChangeDescriptionType[] updates = new ItemChangeDescriptionType[1];

 

           updates[0] = new ItemChangeDescriptionType();

           updates[0].Item = path;

 

           updatedItems[0].Item = new ItemIdType();

 

           ((ItemIdType)updatedItems[0].Item).Id = messageId.Id;

           ((ItemIdType)updatedItems[0].Item).ChangeKey = messageId.ChangeKey;

 

           UpdateItemType request = new UpdateItemType();

 

           request.ItemChanges = updatedItems;

           request.ConflictResolution = ConflictResolutionType.AutoResolve;

           request.MessageDisposition = MessageDispositionType.SaveOnly;

           request.MessageDispositionSpecified = true;

           request.SendMeetingInvitationsOrCancellations = CalendarItemUpdateOperationType.SendToChangedAndSaveCopy;

           request.SendMeetingInvitationsOrCancellationsSpecified = true;

 

           UpdateItemResponseType response = esb.UpdateItem(request);

 

 

           if (response.ResponseMessages.Items[0].ResponseClass != ResponseClassType.Success)

 

           {

               return false;

           }

           else

           {

               return true;

           }

       }

   }

}