示例#1
0
        public IEnumerable <MailMessage> GetPendingMessages()
        {
            var readMails = 0;

            if (!OutlookIsRunning)
            {
                LaunchOutlook();
            }

            var emailsCount = 0;

            InitializeObjects();

            do
            {
                var items = MapiFolder.Items;
                emailsCount = items.Count;

                foreach (var it in items)
                {
                    if (!(it is MailItem item))
                    {
                        continue;
                    }
                    if (!GetSenderSMTPAddress(item).IsValidEmail())
                    {
                        continue;
                    }

                    readMails++;
                    yield return(ParseMessage(item));

                    if (OnMessageRead != null)
                    {
                        var e = new OnMessageReadEventArgs();
                        OnMessageRead(this, e);
                        if (e.Cancel)
                        {
                            continue;
                        }
                    }

                    item.Delete();
                    Thread.Sleep(1000);
                    if (readMails == 10)
                    {
                        break;
                    }
                }
            } while (emailsCount > 0 && (readMails > 0 && readMails < 10));

            DisposeObjects();
        }
        public IEnumerable <MailMessage> GetPendingMessages()
        {
            var inbox    = new ex.FolderId(ex.WellKnownFolderName.Inbox, new ex.Mailbox(Config["NOME_CAIXA"]));
            var itemView = new ex.ItemView(Config["QTD_EMAILS_RECUPERAR"].To(10))
            {
                PropertySet = new ex.PropertySet(ex.BasePropertySet.IdOnly, ex.ItemSchema.Subject,
                                                 ex.ItemSchema.DateTimeReceived)
            };

            itemView.OrderBy.Add(ex.ItemSchema.DateTimeReceived, ex.SortDirection.Ascending);

            var findResults = GetExchangeService(Username, Password).FindItems(inbox, itemView);

            var items = GetExchangeService(Username, Password).BindToItems(findResults.Select(item => item.Id),
                                                                           new ex.PropertySet(ex.BasePropertySet.FirstClassProperties,
                                                                                              ex.EmailMessageSchema.From,
                                                                                              ex.EmailMessageSchema.ToRecipients,
                                                                                              ex.ItemSchema.Attachments,
                                                                                              ex.EmailMessageSchema.CcRecipients,
                                                                                              ex.EmailMessageSchema.BccRecipients,
                                                                                              ex.ItemSchema.Body,
                                                                                              ex.ItemSchema.DateTimeCreated,
                                                                                              ex.ItemSchema.DateTimeReceived,
                                                                                              ex.ItemSchema.DateTimeSent,
                                                                                              ex.ItemSchema.DisplayCc,
                                                                                              ex.ItemSchema.DisplayTo,
                                                                                              ex.ItemSchema.Subject));

            foreach (var item in items)
            {
                yield return(ParseItem(item));

                var ea = new OnMessageReadEventArgs();
                if (OnMessageRead != null)
                {
                    OnMessageRead(this, ea);
                    if (ea.Cancel)
                    {
                        continue;
                    }
                }
                GetExchangeService(Username, Password).DeleteItems(new[] { item.Item.Id }, ex.DeleteMode.MoveToDeletedItems, null, null);
            }
        }