private async Task <IEnumerable <Email> > GetNewEmails()
        {
            var emailsSearchQuery = EmailSearchQuery.Create();

            if (this.lastEmailId != null)
            {
                emailsSearchQuery.FromId(this.lastEmailId.Value);
            }
            else
            {
                emailsSearchQuery.TakeLastNEmails(1);
            }

            var message           = new GetInboxEmails(emailsSearchQuery);
            var newEmailsResponse = await this.inboxEmailActor.Ask <GetInboxEmails.Response>(message);

            switch (newEmailsResponse)
            {
            case GetInboxEmails.Error error:
                throw new Exception("Loading inbox emails error", error.Exception);

            case GetInboxEmails.Success success:
                if (success.Emails.Any())
                {
                    this.lastEmailId = success.Emails.Max(e => e.UniqueId);
                    return(success.Emails);
                }

                return(null);

            default:
                throw new Exception($"Unexpected inbox emails response: {newEmailsResponse.GetType()}");
            }
        }
        private async Task <IEnumerable <Email> > GetEmails(EmailSearchQuery query)
        {
            this.logger.Debug("Loading inbox emails started");

            this.logger.Debug($"Inbox emails query: {query}");

            IEnumerable <Email> emails;

            using (var client = new ImapClient())
            {
                await client.ConnectAsync(
                    this.imapSettings.Host,
                    this.imapSettings.Port);

                await client.AuthenticateAsync(this.imapSettings.User, this.imapSettings.Password);

                await client.Inbox.OpenAsync(FolderAccess.ReadOnly);

                var inboxQuery = new SearchQuery();

                if (!string.IsNullOrWhiteSpace(query.Subject))
                {
                    inboxQuery = inboxQuery.And(SearchQuery.SubjectContains(query.Subject));
                }

                var ids = await client.Inbox.SearchAsync(inboxQuery);

                if (query.MinId != null)
                {
                    ids = ids.Where(x => x.Id > query.MinId).ToList();
                }

                var messages = (await client.Inbox
                                .FetchAsync(ids, MessageSummaryItems.BodyStructure | MessageSummaryItems.Envelope))
                               .OrderByDescending(m => m.Date)
                               .ToList();

                if (!string.IsNullOrWhiteSpace(query.Sender))
                {
                    messages = messages
                               .Where(m => m.Envelope.From.Any(f => f.ToString().Contains(query.Sender)))
                               .ToList();
                }

                if (query.LastNEmails != null)
                {
                    messages = messages
                               .Take((int)query.LastNEmails.Value)
                               .ToList();
                }

                this.logger.Debug($"Total messages loaded: {messages.Count}");

                emails = this.ConvertMessages(client, messages);

                await client.DisconnectAsync(true);
            }

            this.logger.Debug("Loading inbox emails finished");

            return(emails);
        }
示例#3
0
 public GetInboxEmails(EmailSearchQuery emailSearchQuery)
 {
     this.EmailSearchQuery = emailSearchQuery;
 }