public async Task AddEmailsFromEmailAccountToSendeEmailMessagesAsync(string email, string password, string imapHost, int imapPort, DateTime serchAfterSentDate) { var emailAccount = await _emailAccountRepository.GetByEmailAsync(email); var sentEmails = await _emailClientFactory.GetSentEmailsAsync(imapHost, imapPort, email, password, serchAfterSentDate); var afterGivenDateEmails = sentEmails.Where(x => x.Date.Date > serchAfterSentDate.Date); if (afterGivenDateEmails.Count() > 0) { 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.LocalDateTime, item.Subject, item.HtmlBody, true); sendedEmailMessage.AddSendedAtachments(sendedAttachments); } else { sendedEmailMessage = new SendedEmailMessage(item.From.ToString(), item.To.ToString(), item.Date.LocalDateTime, item.Subject, item.HtmlBody, false); } sendedEmailMessage.AddEmailAccount(emailAccount); sentMessages.Add(sendedEmailMessage); } await _sendedEmailMessageRepository.UpdateRangeAsync(sentMessages); } }
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 SendAsync(int userId, string from, ICollection <string> to, ICollection <string> cc, ICollection <string> bcc, string subject, string textHtmlBody, ICollection <IFormFile> attachments = null) { var emailAccountWithSmtpSettings = await _emailAccountRepository.GetByUserIdAndByEmailWithSmtpAsync(userId, from, false); if (emailAccountWithSmtpSettings == null || emailAccountWithSmtpSettings.Smtp == null) { throw new Exception("Email account with this ids does not exist."); } var sendedEmailMessage = new SendedEmailMessage(from, to, subject, textHtmlBody, emailAccountWithSmtpSettings); ICollection <SendedAtachment> sendedAttachments = null; if (attachments != null && attachments.Count > 0) { sendedAttachments = await ConvertFormFilesToSendedAttachments(attachments); sendedEmailMessage.AddSendedAtachments(sendedAttachments); } await _emailClientFactory.SendAsync(emailAccountWithSmtpSettings.Email, emailAccountWithSmtpSettings.Password, emailAccountWithSmtpSettings.Smtp.Host, emailAccountWithSmtpSettings.Smtp.Port, emailAccountWithSmtpSettings.User.Name, to, cc, bcc, subject, textHtmlBody, sendedAttachments); await _sendedEmailMessageRepository.AddAsync(sendedEmailMessage); }