예제 #1
0
        private bool CheckRoomAvailability(string roomName, string email, Slot slot, ExchangeService service, DateTime startDate, DateTime endtDate, Dictionary <string, FindItemsResults <Appointment> > fapts)
        {
            bool   blnReturn = true;
            string key       = email + startDate.ToString("ddMMyyyy") + endtDate.ToString("ddMMyyyy");
            FindItemsResults <Appointment> fapt = fapts[key];

            if (fapt == null)
            {
                return(false);
            }
            if (fapt.Count() > 0)
            {
                var starts = fapt.Select(t => t.Start).ToList();
                var ends   = fapt.Select(t => t.End).ToList();

                if (fapt.Where(t => ((t.Start < slot.StartDateTime && t.End > slot.StartDateTime) || (t.Start < slot.EndDateTime && t.End > slot.EndDateTime) ||
                                     (slot.StartDateTime < t.Start && slot.EndDateTime > t.Start) || (slot.StartDateTime < t.End && slot.EndDateTime > t.End) ||
                                     (slot.StartDateTime == t.Start && slot.EndDateTime == t.End)
                                     )).Count() > 0)
                {
                    return(false);
                }
            }
            return(blnReturn);
        }
        public List <EmailMessage> GetLastEmails(ExchangeService client)
        {
            FindItemsResults <Item> emailItems = client.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
            var emails = emailItems.Select(item => EmailMessage.Bind(client, item.Id)).ToList();

            return(emails);
        }
예제 #3
0
        public IEnumerable <MailItem> GetMailFrom(WellKnownFolderName folderName, int maxItems)
        {
            var view = new ItemView(maxItems)
            {
                Traversal = ItemTraversal.Shallow,
            };

            FindItemsResults <Item> findResults = service.FindItems(folderName, view);

            if (findResults.Count() > 0)
            {
                var itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients, ItemSchema.Attachments)
                {
                    RequestedBodyType = BodyType.Text
                };
                ServiceResponseCollection <GetItemResponse> items =
                    service.BindToItems(findResults.Select(item => item.Id), itempropertyset);
                foreach (var item in items)
                {
                    var emailMessage = item.Item as EmailMessage;
                    var mailItem     = MailItem.FromExchangeMailMessage(emailMessage);

                    yield return(mailItem);
                }
            }
        }
        public IFolderItem ToFolderItem(Folder folder, FindItemsResults <Item> resultItems = null)
        {
            IFolderItem folderItem = ToFolderItem(folder.DisplayName);

            if (resultItems != null)
            {
                resultItems
                .Select(i => ToMailItem(i as EmailMessage))
                .ToList()
                .ForEach(item => folderItem.Messages.Add(item));
            }

            return(folderItem);
        }
        public List <IMailItem> OpenInbox(int?pageSize = null, bool?read = null)
        {
            ItemView itemView = new ItemView(pageSize ?? int.MaxValue);

            SearchFilter.SearchFilterCollection collection =
                new SearchFilter.SearchFilterCollection(LogicalOperator.And);

            if (read.HasValue)
            {
                collection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, read.Value));
            }

            FindItemsResults <Item> homeItems = Service.FindItems(WellKnownFolderName.Inbox, collection, itemView);

            return(new List <IMailItem>(
                       homeItems.Select(i => Factory.ToMailItem(i as EmailMessage)
                                        )));
        }
        public void linkEmails()
        {
            FindItemsResults <Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(128));

            if (findResults.TotalCount > 0)
            {
                ServiceResponseCollection <GetItemResponse> items = service.BindToItems(findResults.Select(item => item.Id), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients));
                foreach (GetItemResponse i in items)
                {
                    MailItem m  = new MailItem();
                    Item     it = i.Item;
                    m.From        = ((Microsoft.Exchange.WebServices.Data.EmailAddress)it[EmailMessageSchema.From]).Address;
                    m.Recipients  = ((Microsoft.Exchange.WebServices.Data.EmailAddressCollection)it[EmailMessageSchema.ToRecipients]).Select(r => r.Address).ToArray();
                    m.Subject     = it.Subject;
                    m.Body        = it.Body.Text;
                    m.Recieved    = it.DateTimeReceived;
                    m.attachments = it.Attachments;
                    foreach (Attachment a in m.attachments)
                    {
                        this.uploadAttachments(a);
                    }
                    i.Item.Delete(DeleteMode.HardDelete);
                }
            }
        }
예제 #7
0
        //private void Get

        private void ExtrairEmail()
        {
            ExchangeService service;

            #region TOBEIMPLEMENTEDAFTER
            //List<FileStream> fileStreams = new List<FileStream>();
            #endregion
            try
            {
                Console.WriteLine("Registering Exchange connection");

                service = new ExchangeService
                {
                    Credentials = new WebCredentials("*****@*****.**", "Ber@2020C@")
                };
            }
            catch
            {
                Console.WriteLine("new ExchangeService failed. Press enter to exit:");
                throw;
            }

            service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

            try
            {
                ItemView view = new ItemView(400, 0, OffsetBasePoint.Beginning);
                view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);

                FindItemsResults <Item> findResults = service.FindItems(WellKnownFolderName.Inbox, view);
                ServiceResponseCollection <GetItemResponse> items = service.BindToItems(findResults.Select(item => item.Id), new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.Subject, EmailMessageSchema.From, EmailMessageSchema.ToRecipients, EmailMessageSchema.CcRecipients, EmailMessageSchema.DateTimeReceived, EmailMessageSchema.Body, ItemSchema.Attachments, EmailMessageSchema.IsRead));
                var sortedItems = items.OrderBy(x => x.Item.DateTimeReceived);
                foreach (var item in sortedItems)
                {
                    EmailMessage message = EmailMessage.Bind(service, new ItemId(item.Item.Id.ToString()), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

                    MailItem mailItem = new MailItem();
                    mailItem.From            = ((Microsoft.Exchange.WebServices.Data.EmailAddress)item.Item[EmailMessageSchema.From]).Address;
                    mailItem.ToRecipients    = ((Microsoft.Exchange.WebServices.Data.EmailAddressCollection)item.Item[EmailMessageSchema.ToRecipients]).Select(recipient => recipient.Address).ToArray();
                    mailItem.CcRecipients    = ((Microsoft.Exchange.WebServices.Data.EmailAddressCollection)item.Item[EmailMessageSchema.CcRecipients]).Select(ccRecipients => ccRecipients.Address).ToArray();
                    mailItem.Subject         = item.Item.Subject;
                    mailItem.DataRecebimento = item.Item.DateTimeReceived;
                    mailItem.Body            = ConvertToPlainText(item.Item.Body.Text.ToString());
                    mailItem.isRead          = Convert.ToBoolean(item.Item[EmailMessageSchema.IsRead].ToString());

                    if (mailItem.Subject.Contains("Controle de ponto") /* &&  !mailItem.isRead*/)
                    {
                        foreach (Microsoft.Exchange.WebServices.Data.Attachment attachment in message.Attachments)
                        {
                            if (attachment is FileAttachment)
                            {
                                FileAttachment fileAttachment = attachment as FileAttachment;
                                fileAttachment.Load();

                                if (!string.IsNullOrEmpty(_caminhoDestino))
                                {
                                    if (fileAttachment.Name.ToUpper().Contains(".XLSM"))
                                    {
                                        string dest     = Path.Combine(_caminhoDestino, fileAttachment.Name);
                                        string fileName = fileAttachment.Name;
                                        int    i        = 1;

                                        while (File.Exists(dest))
                                        {
                                            string previousIterator = "(" + (i - 1) + ")";
                                            fileName = fileName.Replace(".xlsm", "").Replace(previousIterator, "") + "(" + i + ").xlsm";
                                            dest     = Path.Combine(_caminhoDestino, fileName);
                                            i++;
                                        }

                                        dest = Path.Combine(_caminhoDestino, fileName);
                                        fileAttachment.Load(dest);
                                        FileStream fileStream = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                                        fileAttachment.Load(fileStream);
                                        fileStream.Close();
                                        fileStream.Dispose();
                                        #region TOBEIMPLEMENTEDAFTER
                                        //fileStreams.Add(fileStream);
                                        #endregion
                                    }
                                }
                                else
                                {
                                    throw new ClassNotInitializedCorrectly("Para extrair o email você precisa initializar este objeto com o caminho de destino dos arquivos");
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Email lido");
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            #region TOBEIMPLEMENTEDAFTER
            //return fileStreams;
            #endregion
        }
예제 #8
0
        /// <summary>
        /// Check on mail folder last received asuntos
        /// </summary>
        /// <param name="prmLastDateTimeChecked">Last date registered of check to filter folder</param>
        /// <returns></returns>
        public List <Entidades.Asunto> GetLastAsuntosAdded(DateTime prmLastDateTimeChecked)
        {
            // Generate a new asunto list to return
            List <Entidades.Asunto> lstAsuntoToProcess = new List <Entidades.Asunto>();
            // Get filtering properties for search on inbox
            SearchFilter filterCriteria = getFilterForAsuntosSearch(prmLastDateTimeChecked.AddSeconds(10));
            // Get view with specify properties for search
            ItemView viewContainer = getResultViewForAsuntoList();
            // Generate a collection for matching items
            FindItemsResults <Item> resultOfSearch = mailServiceConnection.FindItems(asuntoFolder, filterCriteria, viewContainer);

            if (resultOfSearch.TotalCount > 0)
            {
                // Generate a new ServiceResponseCollection with items saved
                ServiceResponseCollection <GetItemResponse> itemWithBodyAndDateReceived = mailServiceConnection.BindToItems(resultOfSearch.Select(item => item.Id), new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.DateTimeReceived));
                // Iterates over all results finded
                foreach (var item in itemWithBodyAndDateReceived.Select(item => item.Item).ToArray())
                {
                    // Save temporaly in a variable subject result
                    string subjectWithAsunto = item.Subject;
                    // Get Start point for cutting string resullt
                    int intStartCutString = subjectWithAsunto.IndexOf(startSubstringCut) + startSubstringCut.Length;
                    int intEndCutString   = subjectWithAsunto.LastIndexOf(endSubstringCut);
                    // With end and start can obtain asunto number
                    string asuntoNumber     = subjectWithAsunto.Substring(intStartCutString, (intEndCutString - intStartCutString));
                    string shortDescription = getShortDescriptionByFiltering(item.Body);
                    // Generate a new entidades of asunto and add to list
                    lstAsuntoToProcess.Add(new Entidades.Asunto()
                    {
                        Numero = asuntoNumber, DescripcionBreve = shortDescription, LoadedOnSolucionameDate = item.DateTimeReceived
                    });
                }
            }
            // Return processed list
            return(lstAsuntoToProcess);
        }