private MailMessage ParseItem(ex.GetItemResponse item)
        {
            MailMessage mailMessage = new MailMessage();
            var exchangeSenderEmail = (Microsoft.Exchange.WebServices.Data.EmailAddress)item.Item[ex.EmailMessageSchema.From];
            mailMessage.From = new MailAddress(exchangeSenderEmail.Address, exchangeSenderEmail.Name);

            foreach (var emailAddress in ((Microsoft.Exchange.WebServices.Data.EmailAddressCollection)item.Item[ex.EmailMessageSchema.ToRecipients]))
            {
                mailMessage.To.Add(new MailAddress(emailAddress.Address, emailAddress.Name));
            }


            mailMessage.Subject = item.Item.Subject;
            mailMessage.Body = item.Item.Body.ToString();
            var attachmentCollection = item.Item[ex.EmailMessageSchema.Attachments];


            foreach (ex.Attachment attachment in (ex.AttachmentCollection)attachmentCollection)
            {
                if (attachment is ex.FileAttachment)
                {
                    ex.FileAttachment fileAttachment = (ex.FileAttachment)attachment;
                    fileAttachment.Load();

                    var tempFile = Path.GetTempFileName();
                    File.WriteAllBytes(tempFile, fileAttachment.Content);
                    MemoryStream ms = new MemoryStream(File.ReadAllBytes(tempFile));
                    mailMessage.Attachments.Add(new System.Net.Mail.Attachment(ms, attachment.Name ?? tempFile));
                    File.Delete(tempFile);
                }
            }

            return mailMessage;
        }
        private static webServiceData.Appointment getCurrentAppointment(SchedulingInfo schedulingInfo, webServiceData.ExchangeService service)
        {
            webServiceData.Appointment theCurrentAppointment = null;
            //service.ImpersonatedUserId = new webServiceData.ImpersonatedUserId(webServiceData.ConnectingIdType.SmtpAddress, schedulingInfo.senderEmailAddr);

            //long startSearchTime = DateTime.UtcNow.Ticks;
            //do
            //{
                if (schedulingInfo.meetingType == MeetingType.Ocurrence)
                {
                    webServiceData.Appointment appointment = searchAppointment(schedulingInfo, service);
                    if (null != appointment)
                    {
                        theCurrentAppointment = appointment;
                    }
                }
                else
                {
                    webServiceData.Appointment appointment = searchAppointmentByFilter(schedulingInfo, service, null);
                    if (null != appointment)
                    {
                        theCurrentAppointment = appointment;
                    }
                }

            //    if (null == theCurrentAppointment) Thread.Sleep(5000);
            //} while (null == theCurrentAppointment && (DateTime.UtcNow.Ticks - startSearchTime) / 10000000 <= 60);

            if (null == theCurrentAppointment)
            {
                //do
                //{
                    webServiceData.Appointment appointment = searchAppointment(schedulingInfo, service);
                    //if (null != appointment)
                    //{
                        theCurrentAppointment = appointment;
                    //}
                    //else
                    //{
                    //    Thread.Sleep(5000);
                    //}
                //} while (null == theCurrentAppointment && (DateTime.UtcNow.Ticks - startSearchTime) / 10000000 <= 10);
            }

            return theCurrentAppointment;
        }
        private static icm.XmlApi.dayRecurrenceType convertToDayRecurrenceType(webServiceData.DayOfTheWeek whichDay)
        {
            icm.XmlApi.dayRecurrenceType result = icm.XmlApi.dayRecurrenceType.MON;
            if ( webServiceData.DayOfTheWeek.Day == whichDay )
                result = icm.XmlApi.dayRecurrenceType.ANYDAY;
            else if ( webServiceData.DayOfTheWeek.Weekday == whichDay )
                result = icm.XmlApi.dayRecurrenceType.WEEKDAY;
            else if ( webServiceData.DayOfTheWeek.WeekendDay == whichDay )
                result = icm.XmlApi.dayRecurrenceType.WEEKENDDAY;
            else if (webServiceData.DayOfTheWeek.Tuesday == whichDay)
                result = icm.XmlApi.dayRecurrenceType.TUE;
            else if (webServiceData.DayOfTheWeek.Wednesday == whichDay)
                result = icm.XmlApi.dayRecurrenceType.WED;
            else if (webServiceData.DayOfTheWeek.Thursday == whichDay)
                result = icm.XmlApi.dayRecurrenceType.THU;
            else if (webServiceData.DayOfTheWeek.Friday == whichDay)
                result = icm.XmlApi.dayRecurrenceType.FRI;
            else if (webServiceData.DayOfTheWeek.Saturday == whichDay)
                result = icm.XmlApi.dayRecurrenceType.SAT;
            else if (webServiceData.DayOfTheWeek.Sunday == whichDay)
                result = icm.XmlApi.dayRecurrenceType.SUN;

            return result;
        }
 private static void updateAppointment(webServiceData.Appointment appointment)
 {
     int j = 3;
     do
     {
         try
         {
             appointment.Update(webServiceData.ConflictResolutionMode.AlwaysOverwrite, webServiceData.SendInvitationsOrCancellationsMode.SendToNone);
             break;
         }
         catch (Exception ex)
         {
             RvLogger.DebugWrite("Exception==============" + ex.Message);
             RvLogger.DebugWrite(ex.StackTrace);
         }
         j--;
     } while (j >= 0);
 }
        private static void saveConferenceInfoIntoAppointment(icm.XmlApi.scheduleReportType result, webServiceData.Appointment appointment, SchedulingInfo schedulingInfo, webServiceData.ExchangeService service, string dialingInfoStr, string uniqueId)
        {
                string hashString = RvScopiaMeeting.GetHashString(schedulingInfo, schedulingInfo.subject);
                if (schedulingInfo.requestType != RequestType.CancelMeeting)
                {
                    string conferenceID = null == result || !result.Success ? "empty" : result.ConferenceId;
                    if (isCreateMeetingRequest(schedulingInfo))
                    {
                        if (!result.Success && IS_DELETE_ERROR) {
                            appointment.Delete(webServiceData.DeleteMode.HardDelete, webServiceData.SendCancellationsMode.SendToNone);
                            RvLogger.DebugWrite("delete appointment: " + schedulingInfo.subject);
                            return;
                        }
                        appointment.MeetingWorkspaceUrl = CONFERENCE_ID + ":" + conferenceID + ":" + hashString + "$" + schedulingInfo.recurrenceHashInfo;
                        if (USE_HTML)
                            appointment.Body.BodyType = webServiceData.BodyType.HTML;

                        List<webServiceData.Attachment> attachments = new List<webServiceData.Attachment>(appointment.Attachments.Count);
                        foreach (webServiceData.Attachment attachment in appointment.Attachments)
                            appointment.Body.Text = appointment.Body.Text.Replace(attachment.Name, "");
                        appointment.Body.Text = appointment.Body + Environment.NewLine + "<br>" + dialingInfoStr;
                        if (schedulingInfo.meetingType == MeetingType.Reccurence)
                            appointment.MeetingWorkspaceUrl = "#" + appointment.MeetingWorkspaceUrl;
                    }
                    else
                    {
                        if (!result.Success && IS_DELETE_ERROR) {
                            appointment.Delete(webServiceData.DeleteMode.HardDelete, webServiceData.SendCancellationsMode.SendToNone);
                            RvLogger.DebugWrite("delete appointment: " + schedulingInfo.subject);
                            return;
                        }
                    }                    
                }

                updateAppointment(appointment);
        }
        private static void updateAppointment(webServiceData.Appointment appointment, SchedulingInfo schedulingInfo, webServiceData.ExchangeService service, string dialingInfoStr, icm.XmlApi.scheduleReportType result)
        {
                if (null == appointment) 
                    return;

                string uniqueId = null;
                if (schedulingInfo.meetingType == MeetingType.Ocurrence)
                {
                    appointment = webServiceData.Appointment.Bind(service, appointment.Id, new webServiceData.PropertySet(webServiceData.BasePropertySet.FirstClassProperties) { RequestedBodyType = webServiceData.BodyType.HTML });
                }
                else if (appointment.AppointmentType == webServiceData.AppointmentType.RecurringMaster)
                {
                    uniqueId = appointment.Id.UniqueId;
                    appointment = webServiceData.Appointment.Bind(service, appointment.Id);
                }
                else
                    appointment = webServiceData.Appointment.Bind(service, appointment.Id);

                saveConferenceInfoIntoAppointment(result, appointment, schedulingInfo, service, dialingInfoStr, uniqueId);
        }
        private static webServiceData.Appointment searchAppointmentByFilter(SchedulingInfo schedulingInfo, webServiceData.ExchangeService service, string appointmentID)
        {
            RvLogger.DebugWrite("enter searchAppointmentByFilter==============");
            List<webServiceData.SearchFilter> searchORFilterCollection = new List<webServiceData.SearchFilter>();
            if (null != appointmentID)
                searchORFilterCollection.Add(new webServiceData.SearchFilter.IsEqualTo(webServiceData.EmailMessageSchema.Id, new webServiceData.ItemId(appointmentID)));
            else {
                searchORFilterCollection.Add(new webServiceData.SearchFilter.IsEqualTo(webServiceData.EmailMessageSchema.Subject, schedulingInfo.subject));
                searchORFilterCollection.Add(new webServiceData.SearchFilter.IsEqualTo(webServiceData.EmailMessageSchema.From, schedulingInfo.delegatorEmailAddr));
                searchORFilterCollection.Add(new webServiceData.SearchFilter.IsGreaterThan(webServiceData.EmailMessageSchema.LastModifiedTime, DateTime.UtcNow.AddHours(-25)));
                if (!string.IsNullOrEmpty(schedulingInfo.storeEntryId))
                    searchORFilterCollection.Add(new webServiceData.SearchFilter.IsEqualTo(webServiceData.EmailMessageSchema.StoreEntryId, schedulingInfo.storeEntryId));
                if (!string.IsNullOrEmpty(schedulingInfo.conversationKey))
                    searchORFilterCollection.Add(new webServiceData.SearchFilter.IsEqualTo(webServiceData.EmailMessageSchema.ConversationId, schedulingInfo.conversationKey));
            }
            RvLogger.DebugWrite("enter searchAppointmentByFilter==============1");
            webServiceData.SearchFilter searchFilter = new webServiceData.SearchFilter.SearchFilterCollection(webServiceData.LogicalOperator.And, searchORFilterCollection.ToArray());
            RvLogger.DebugWrite("enter searchAppointmentByFilter==============2 " + schedulingInfo.delegatorEmailAddr);
            webServiceData.Mailbox mailBox = new webServiceData.Mailbox(schedulingInfo.delegatorEmailAddr);
            RvLogger.DebugWrite("enter searchAppointmentByFilter==============3");
            webServiceData.FolderId folderID = new webServiceData.FolderId(webServiceData.WellKnownFolderName.Calendar, mailBox); //No need to set mail since the service already know it.
            //webServiceData.FolderId folderID = new webServiceData.FolderId(webServiceData.WellKnownFolderName.Calendar);
            RvLogger.DebugWrite("enter searchAppointmentByFilter==============4");
            webServiceData.FindItemsResults<webServiceData.Item> results = service.FindItems(
                                    folderID,
                                    searchFilter,
                                    new webServiceData.ItemView(100));
            RvLogger.DebugWrite("enter searchAppointmentByFilter==============5");
            RvLogger.DebugWrite("results searchAppointmentByFilter==============" + (null == results.Items ? "0" : "" + results.Items.Count));

            foreach (webServiceData.Item item in results) {
                try {
                    webServiceData.Appointment appointment = (webServiceData.Appointment)item;
                    if (string.IsNullOrEmpty(schedulingInfo.location)) schedulingInfo.location = "";
                    if (string.IsNullOrEmpty(appointment.Location)) appointment.Location = "";
                    if (schedulingInfo.location == appointment.Location
                        && schedulingInfo.startDate.ToUniversalTime().Equals(appointment.Start.ToUniversalTime())
                        && schedulingInfo.endDate.ToUniversalTime().Equals(appointment.End.ToUniversalTime()))
                    {
                        RvLogger.DebugWrite("lastModifiedTime1===================" + appointment.LastModifiedTime);
                        return appointment;
                    }
                }
                catch (ScopiaMeetingAddInException ex) {
                    throw ex;
                }
            }

            return null;
        }
        private static webServiceData.Appointment searchAppointment(SchedulingInfo schedulingInfo, webServiceData.ExchangeService service)
        {
            RvLogger.DebugWrite("enter searchAppointment==============");
            webServiceData.Mailbox mailBox = new webServiceData.Mailbox(schedulingInfo.delegatorEmailAddr);
            webServiceData.FolderId folderID = new webServiceData.FolderId(webServiceData.WellKnownFolderName.Calendar, mailBox);
            webServiceData.CalendarFolder folder = webServiceData.CalendarFolder.Bind(service, folderID);
            webServiceData.CalendarView view = new webServiceData.CalendarView(schedulingInfo.startDate, schedulingInfo.endDate);
            webServiceData.PropertySet propertySet = new webServiceData.PropertySet(webServiceData.BasePropertySet.FirstClassProperties);
            view.PropertySet = propertySet;
            webServiceData.FindItemsResults<webServiceData.Appointment> results = folder.FindAppointments(view);

            RvLogger.DebugWrite("results==============" + (null == results.Items ? "0" : "" + results.Items.Count));

            foreach (webServiceData.Item item in results)
            {
                try
                {
                    webServiceData.Appointment appointment = (webServiceData.Appointment)item;
                    if (string.IsNullOrEmpty(schedulingInfo.location)) schedulingInfo.location = "";
                    if (string.IsNullOrEmpty(appointment.Location)) appointment.Location = "";
                    if (string.IsNullOrEmpty(schedulingInfo.subject)) schedulingInfo.subject = "";
                    if (string.IsNullOrEmpty(appointment.Subject)) appointment.Subject = "";
                    if (schedulingInfo.location == appointment.Location 
                        && appointment.Subject == schedulingInfo.subject
                        && 0 == appointment.Start.ToUniversalTime().CompareTo(schedulingInfo.startDate.ToUniversalTime())
                        && 0 == appointment.End.ToUniversalTime().CompareTo(schedulingInfo.endDate.ToUniversalTime()))
                    {
                        return appointment;
                    }
                }
                catch (ScopiaMeetingAddInException ex)
                {
                    throw ex;
                }
            }

            return null;
        }