public async Task AddEmailsFromEmailAccountToEmailMessagesAsync(string email, string password, string imapHost, int imapPort, DateTime serchAfterDeliveredDate) { var emailAccount = await _emailAccountRepository.GetByEmailAsync(email); var deliveredEmails = await _emailClientFactory.GetInboxEmailsAsync(imapHost, imapPort, email, password, serchAfterDeliveredDate); var afterGivenDateEmails = deliveredEmails.Where(x => x.Date.LocalDateTime > serchAfterDeliveredDate); if (afterGivenDateEmails.Count() > 0) { ICollection <EmailMessage> deliveredMessages = new List <EmailMessage> (); foreach (var item in afterGivenDateEmails) { EmailMessage emailMessage; if (item.Attachments != null && item.Attachments.Count() > 0) { emailMessage = new EmailMessage(item.From.ToString(), item.To.ToString(), item.Date, item.Subject, item.HtmlBody, true); ICollection <Atachment> atachments = GetAttachmentsFromMmimeMessage(item); emailMessage.AddAtachments(atachments); } else { emailMessage = new EmailMessage(item.From.ToString(), item.To.ToString(), item.Date, item.Subject, item.HtmlBody, false); } emailMessage.AddEmailAccount(emailAccount); deliveredMessages.Add(emailMessage); } await _emailMessageRepository.AddRangeAsync(deliveredMessages); } }
public async Task GetEmailsFromEmailAccountAsync(EmailAccount emailAccount, string impaHost, int imapPort) { var deliveredEmails = await _emailClientFactory.GetInboxEmailsAsync(impaHost, imapPort, emailAccount.Email, emailAccount.Password, DateTime.UtcNow.AddDays(-14)); ICollection <EmailMessage> deliveredMessages = new List <EmailMessage> (); foreach (var item in deliveredEmails) { EmailMessage emailMessage; if (item.Attachments != null && item.Attachments.Count() > 0) { emailMessage = new EmailMessage(item.From.ToString(), item.To.ToString(), item.Date.UtcDateTime, item.Subject, item.HtmlBody, true); ICollection <Atachment> atachments = GetAttachmentsFromMmimeMessage(item); emailMessage.AddAtachments(atachments); } else { emailMessage = new EmailMessage(item.From.ToString(), item.To.ToString(), item.Date.UtcDateTime, item.Subject, item.HtmlBody, false); } emailMessage.AddEmailAccount(emailAccount); deliveredMessages.Add(emailMessage); } await _emailMessageRepository.AddRangeAsync(deliveredMessages); var sentEmails = await _emailClientFactory.GetSentEmailsAsync(impaHost, imapPort, emailAccount.Email, emailAccount.Password, DateTime.UtcNow.AddDays(-14)); ICollection <SendedEmailMessage> sentMessages = new List <SendedEmailMessage> (); foreach (var item in sentEmails) { SendedEmailMessage sendedEmailMessage; if (item.Attachments != null && item.Attachments.Count() > 0) { ICollection <SendedAtachment> sendedAttachments = GetSendedAttachmentsFromMmimeMessage(item); sendedEmailMessage = new SendedEmailMessage(item.From.ToString(), item.To.ToString(), item.Date.UtcDateTime, item.Subject, item.HtmlBody, true); sendedEmailMessage.AddSendedAtachments(sendedAttachments); } else { sendedEmailMessage = new SendedEmailMessage(item.From.ToString(), item.To.ToString(), item.Date.UtcDateTime, item.Subject, item.HtmlBody, false); } sendedEmailMessage.AddEmailAccount(emailAccount); sentMessages.Add(sendedEmailMessage); } await _sendedEmailMessageRepository.AddRangeAsync(sentMessages); }
public async Task AddNewEmailsToDBAsync() { var mimeEmails = await _emailFactory.GetNewEmailsAsync(); foreach (var item in mimeEmails) { EmailMessage emailMessage = new EmailMessage(item.From.ToString(), item.To.ToString(), item.Date.UtcDateTime, item.Subject, item.HtmlBody); ICollection <Atachment> atachments = GetAttachmentsFromMmimeMessage(item); emailMessage.AddAtachments(atachments); await _emailRepository.AddAsync(emailMessage); } await Task.CompletedTask; }