private void QueueUpEmails() { // https://blog.sqltreeo.com/read-emails-from-exchange-online-mailbox-office-365-into-sql-server/ // https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2016); service.Credentials = new WebCredentials(this.emailAddress, this.password); // NOTE: this is for exchange and is slow service.AutodiscoverUrl(this.emailAddress, RedirectionUrlValidationCallback); service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"); //FolderId inbox = new FolderId(WellKnownFolderName.Inbox, mailbox); //archFilter searchFilter = new SearchFilter.IsGreaterThan(ItemSchema.DateTimeSent, DateTime.Now); //SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)) //ItemView view = new ItemView(maxDequeueCount); // take 10 items //FindItemsResults<Item> emails = service.FindItems(inbox, searchFilter, view); FindItemsResults <Item> emails = null; ItemView view = new ItemView(maxDequeueCount); view.OrderBy.Add(ItemSchema.DateTimeReceived, this.sortDirection); if (null == this.filter) { emails = service.FindItems(this.folder, view); } else { emails = service.FindItems(this.folder, this.filter, view); } timeSinceLastServerQuery = DateTime.Now; if (emails.Any()) { service.LoadPropertiesForItems(emails, PropertySet.FirstClassProperties); } foreach (var item in emails) { emailQueue.Enqueue(item); } }
public List <EventsItem> LoadAppointments(string email, string password) { List <EventsItem> eventsItems = new List <EventsItem>(); DateTime startDate = DateTime.Now; DateTime endDate = startDate.AddDays(7); try { CalendarFolder calendar = FindNamedCalendarFolder("officehours", email, password); // or //CalendarFolder calendar = FindDefaultCalendarFolder(email, password); CalendarView cView = new CalendarView(startDate, endDate, 50); cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.When, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.Id); FindItemsResults <Appointment> appointments = calendar.FindAppointments(cView); if (appointments != null && appointments.Any()) { foreach (var appt in appointments) { eventsItems.Add( new EventsItem { Id = appt.Id.ToString(), Subject = appt.Subject, EventDate = DateTime.Parse(appt.Start.ToString()).Date, EventDay = DateTime.Parse(appt.Start.ToString()).DayOfWeek, EventStart = DateTime.Parse(appt.Start.ToString()).ToString("t"), EventEnd = DateTime.Parse(appt.End.ToString()).ToString("t"), }); } } return(eventsItems); } catch { throw; } }
public List <JsonEventsSchedule> GetAppointments(string email, string password) { List <JsonEventsSchedule> eventsItems = new List <JsonEventsSchedule>(); DateTime startDate = DateTime.Now.Date; DateTime endDate = startDate.AddDays(30); try { CalendarFolder calendar = FindNamedCalendarFolder("officehours", email, password); // or //CalendarFolder calendar = FindDefaultCalendarFolder(email, password); CalendarView cView = new CalendarView(startDate, endDate, 50); cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.When, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.Id); FindItemsResults <Appointment> appointments = calendar.FindAppointments(cView); if (appointments != null && appointments.Any()) { foreach (var appt in appointments) { eventsItems.Add( new JsonEventsSchedule { id = appt.Id.ToString(), title = appt.Subject, start = ConvertToiso8601(appt.Start), end = ConvertToiso8601(appt.End), }); } } return(eventsItems); } catch { throw; } }
/// <summary> /// /// </summary> /// <returns></returns> public static ObservableCollection <ExchangeData> GetUserAppointments() { ObservableCollection <ExchangeData> lstData = new ObservableCollection <ExchangeData>(); try { if (_globalService == null) { _globalService = GetExchangeServiceObject(new TraceListner()); } if (!_globalService.HttpHeaders.ContainsKey("X-AnchorMailbox")) { _globalService.HttpHeaders.Add("X-AnchorMailbox", User); } else { _globalService.HttpHeaders["X-AnchorMailbox"] = User; } _globalService.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, User); // AppointmentSchema.Id PropertySet basicProps = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Start, AppointmentSchema.ICalUid, ItemSchema.Subject, ItemSchema.Id, AppointmentSchema.Organizer, ItemSchema.Categories); DateTime dtStart = DateTime.Now.AddDays(BackDays); CalendarView calView = new CalendarView(dtStart, dtStart.AddYears(2)) { Traversal = ItemTraversal.Shallow, PropertySet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Start), MaxItemsReturned = 500 }; List <Appointment> appointments = new List <Appointment>(); FindItemsResults <Appointment> userAppointments = _globalService.FindAppointments(WellKnownFolderName.Calendar, calView); if ((userAppointments != null) && (userAppointments.Any())) { appointments.AddRange(userAppointments); while (userAppointments.MoreAvailable) { calView.StartDate = appointments.Last().Start; userAppointments = _globalService.FindAppointments(WellKnownFolderName.Calendar, calView); appointments.AddRange(userAppointments); } ServiceResponseCollection <ServiceResponse> response = _globalService.LoadPropertiesForItems(appointments, basicProps); if (response.OverallResult != ServiceResult.Success) // property load not success { return(null); } if (appointments?.Count > 0) { foreach (Appointment item in appointments) { lstData.Add(new ExchangeData { UniqueId = item.Id.UniqueId, StartDate = item.Start.ToString("dd-MM-yyyy HH:mm"), Subject = item.Subject, Organizer = item.Organizer.Address, Categories = item.Categories.ToString(), IsSelected = (item.Subject.StartsWith("Canceled:", StringComparison.OrdinalIgnoreCase) || item.Subject.StartsWith("Abgesagt:", StringComparison.OrdinalIgnoreCase)) }); } //group by with Subject, start date var duplicates = from p in lstData.Where(p => !p.IsSelected) group p by new { p.Subject, p.StartDate } into q where q.Count() > 1 select new ExchangeData() { Subject = q.Key.Subject, StartDate = q.Key.StartDate }; var exchangeDatas = duplicates as ExchangeData[] ?? duplicates.ToArray(); if (exchangeDatas?.Length > 0) { foreach (var item in lstData.Where(p => !p.IsSelected)) { if (exchangeDatas.Any(p => string.Equals(p.Subject, item.Subject) && string.Equals(p.StartDate, item.StartDate))) { item.IsSelected = true; } } } } } } catch (Exception e) { WriteLog(e.Message); } return(lstData); }