private async Task SyncMailFolder(EmailConfiguration emailConfiguration, IMailFolder mailFolder, EmailType emailType) { try { //await _cqLogger.LogToFile($"Start syncing {mailFolder.Name} folder from {emailConfiguration.Email}", _level); await mailFolder.OpenAsync(FolderAccess.ReadOnly); //await _cqLogger.LogToFile($"{emailConfiguration.Email} => {mailFolder.Name} is open", _level); #region load completely IList <UniqueId> uids = mailFolder.Search(SearchQuery.All); //await _cqLogger.LogToFile($"{uids.Count} mail is found in {mailFolder.Name} ({emailConfiguration.Email})", _level); int counter = 1; foreach (var uid in uids) { try { //await _cqLogger.LogToFile($"fetching {counter++} email with id {uid} ({emailConfiguration.Email} => {mailFolder.Name})", _level); if (await _emailRepository.ExistenceCheckByUniqueId(uid.ToString(), emailConfiguration.Id)) { //await _cqLogger.LogToFile($"email id {uid} is found in {emailConfiguration.Email}", _level); continue; } var message = await mailFolder.GetMessageAsync(uid); var attachments = await EmailAssistantExtensions.GetAllAttachments(message, emailConfiguration.Email, _pathToSaveFiles); var emailMessage = EmailAssistantExtensions.CreateNew(message, emailConfiguration.Id, uid.ToString(), emailType, EmailStatus.Received); var lstMailAddresses = EmailAssistantExtensions.ExtractAllMailAddresses(message); foreach (var mail in lstMailAddresses) { long contactId = 0; var contact = await _contactRepository.GetByEmail(mail.Name); if (contact == null) { var name = mail.Name.GetName(); var newContact = new Contact { MailAddress = mail.Name, Name = name }; contactId = await _contactRepository.InsertAsync(newContact); } else { contactId = contact.Id; } if (emailMessage.Id < 1) { await _emailRepository.InsertAsync(emailMessage); } var emailContact = new EmailContact { ContactId = contactId, EmailId = emailMessage.Id, RelationType = mail.Type }; await _emailContactRepository.InsertAsync(emailContact); } foreach (var attachment in attachments) { attachment.EmailId = emailMessage.Id; await _attachmentRepository.InsertAsync(attachment); } } catch (Exception e) { await _cqLogger.LogToFile(e.ToString(), _level); } } #endregion } catch (Exception e) { await _cqLogger.LogToFile(e.ToString(), _level); } }