Пример #1
0
        public async Task FilterUids()
        {
            var filter = new ImapFilter().Uids(RetrievalConfiguration.UidsToRetrieve);
            var list   = await Mail2DB.GetUIds(filter);

            list?.Count.Should().BeGreaterThan(0);
        }
Пример #2
0
        public async Task FilterDeliveredBetween()
        {
            var filter = new ImapFilter().DeliveredBetween(RetrievalConfiguration.RetrieveFrom, RetrievalConfiguration.RetrieveTo);
            var list   = await Mail2DB.GetUIds(filter);

            list?.Count.Should().BeGreaterThan(0);
        }
Пример #3
0
 public RetrievalTests()
 {
     Credentials            = ConfigHelper.GetCredentials();
     RetrievalConfiguration = ConfigHelper.GetRetrievalConfiguration();
     Mail2DB       = new GenericClient(Credentials);
     Converter     = new MailTypeConverter(Mail2DB);
     DefaultFilter = new ImapFilter().NotSeen(); // younger than two days
 }
Пример #4
0
        public virtual async Task <IList <IMessageSummary> > GetSummaries(ImapFilter filter, uint[] uidsToExclude = null)
        {
            var inbox = await AuthenticateAsync();

            await inbox.OpenAsync(FolderAccess.ReadOnly);

            _lastRetrievedUIds = await GetUIds(filter);

            if (uidsToExclude?.Length > 0)
            {
                _lastRetrievedUIds = _lastRetrievedUIds.Where(x => !uidsToExclude.Contains(x.Id)).ToArray();
            }
            return(await inbox.FetchAsync(_lastRetrievedUIds, MessageSummaryOptions));
        }
Пример #5
0
        public async Task GetAllMailsAsync()
        {
            _retrievalClient.SetMailFolder(_settings.MailFolderName);
            var uIds = await _retrievalClient.GetUIds(new ImapFilter());

            var existingMails = await _repo.GetExistingUidsAsync(_settings.MailFolderName);

            var        existingUids = existingMails.Select(x => x.UniqueId).ToList();
            ImapFilter uIdFilter    = new();

            if (existingUids.Count > 0)
            {
                uIdFilter = new ImapFilter().Uids(uIds.Where(x => !existingUids.Contains(x.Id)).ToList());
            }

            await foreach (var imapMessage in _mailConverter.GetMessagesAsync(uIdFilter))
            {
                try
                {
                    var email = await _repo.SaveEmailAsync(imapMessage);

                    if (email == null)
                    {
                        _logger.LogInformation($"Email from imap folder '{imapMessage.MailFolder}', uId '{imapMessage.UId}' could not be saved to database.");
                        continue;
                    }

                    var fullFileName = Path.Combine(_settings.DownloadDirectory, EXPORT_PATH, CleanUpMailFolderName(_settings.MailFolderName), email.FileName + IME_FILE_EXTENSION);

                    if (!await _fileSystem.TryWriteAllBytesAsync(fullFileName, imapMessage.MimeMessageBytes))
                    {
                        await _repo.DeleteEmailAsync(email);

                        _logger.LogInformation($"Email from imap folder '{imapMessage.MailFolder}', uId '{imapMessage.UId}' could not be saved to disc.");
                    }
                }
                catch (System.Exception ex)
                {
                    _logger.LogError(ex.Message);
                }
            }
        }
Пример #6
0
        public virtual async Task <IList <UniqueId> > GetUIds(ImapFilter filter = null)
        {
            _mailFolder = await AuthenticateAsync();

            await _mailFolder.OpenAsync(FolderAccess.ReadOnly);

            if (filter == null)
            {
                _lastRetrievedUIds = await _mailFolder.SearchAsync(new SearchQuery());
            }
            else
            {
                _lastRetrievedUIds = await _mailFolder.SearchAsync(filter.ToSearchQuery());
            }

            if (_lastRetrievedUIds == null)
            {
                _lastRetrievedUIds = new List <UniqueId>();
            }

            return(_lastRetrievedUIds);
        }
Пример #7
0
        public static ImapFilter ToIntervalFilter(this AppSettings settings)
        {
            var filter = new ImapFilter().SentBetween(settings.DelieveredAfter, settings.DelieveredBefore);

            return(filter);
        }
Пример #8
0
        public virtual async Task DeleteMessages(ImapFilter imapFilter)
        {
            var uids = await GetUIds(imapFilter);

            await DeleteMessages(uids);
        }
Пример #9
0
        private async Task ProcessAccount(Credentials imapCreds, DataAccount account)
        {
            var mail2db          = new Client(imapCreds);
            var converter        = new MailTypeConverter(mail2db);
            var availableFolders = await mail2db.GetMailFolders();

            var folders = mailFolders.IntersectCaseIgnore(availableFolders).ToList();
            var filter  = new ImapFilter();

            foreach (var mailFolder in folders)
            {
                if (InvalidMailfolder(mailFolder))
                {
                    continue;
                }

                try {
                    mail2db.SetMailFolder(mailFolder);
                    var countInFolder = await mail2db.GetTotalMailCount();

                    if (mail2db.OpenedMailFolder.IsNullOrEmpty())
                    {
                        continue;
                    }
                    Console.WriteLine($"{account.EmailAddress}  mail folder: '{mail2db.OpenedMailFolder}'");
                    Console.WriteLine($"Total Count: {countInFolder}");
                    var savedMessages = await db.Query(SqlBase <DataMessage> .TableName)
                                        .Select(nameof(DataMessage.EmailAccountId), nameof(DataMessage.UId), nameof(DataMessage.MailFolder))
                                        .Where(new { EmailAccountId = account.Id, MailFolder = mailFolder })
                                        .GetAsync <DataMessage>();

                    Console.WriteLine("Existing Uids: {0}", savedMessages.Count());
                    Console.WriteLine("===================\n");

                    converter.UIdsToExclude = savedMessages.Select(x => x.UId).ToArray();

                    async Task ProcessImapMessage(ImapMessage item)
                    {
                        //var TA = db.Connection.BeginTransaction();
                        Console.WriteLine("Saving: {0}\t{1}\"{2}\"", item?.ReceivedAtLocal,
                                          SpacePadding(item?.From?.EmailAddress),
                                          item?.Subject);
                        try {
                            if (!savedMessages.MessageExists(item.UId, item.MailFolder, account.Id))
                            {
                                var msgId = await db.Query(SqlBase <DataMessage> .TableName).InsertGetIdAsync <uint>(item.ToData(account.Id));

                                foreach (var contact in item.ToDataContact(msgId))
                                {
                                    await db.Query(SqlBase <DataContact> .TableName).InsertAsync(contact);
                                }
                                if (item.HasAttachments)
                                {
                                    foreach (var file in item.ToAttachment(msgId))
                                    {
                                        Console.WriteLine("Saving Attachment: {0}", file.Filename);
                                        await db.Query(SqlBase <DataAttachment> .TableName).InsertAsync(file);
                                    }
                                }
                            }
                            //TA.Commit();
                        } catch (Exception ex) {
                            //TA.Rollback();
                            Console.WriteLine(ex.Message);
                        }
                    }

                    await converter.GetMessagesAsync(ProcessImapMessage, filter);
                } catch (Exception ex2) {
                    Console.WriteLine(ex2.Message);
                }
                GC.Collect();
                Console.WriteLine("\n");
            }
        }