Пример #1
0
        public async Task CreateAsync(int userId, string email, string password, string imapHost, int imapPort, string smtpHost, int smtpPort)
        {
            if (!await EmailAccountValuesValidator.EmailAccountUsingImapIsValid(imapHost, imapPort, email, password))
            {
                throw new Exception("Invalid imap email account's credentials or check your email settings");
            }
            if (!await EmailAccountValuesValidator.EmailAccountUsingSmtpIsValid(smtpHost, smtpPort))
            {
                throw new Exception("Invalid smtp email account's credentials or check your email settings");
            }
            var emailAccount = new EmailAccount(email, password);
            var user         = await _userRepository.GetAsync(userId);

            emailAccount.AddUser(user);
            var imap = await _imapRepository.GetAsyncByPort(imapPort);

            if (imap != null)
            {
                emailAccount.AddImapSettings(imap);
            }
            else
            {
                emailAccount.AddImapSettings(new Imap(imapPort, imapHost));
            }
            var smtp = await _smtpRepository.GetAsyncByPort(smtpPort);

            if (smtp != null)
            {
                emailAccount.AddSmtpSettings(smtp);
            }
            else
            {
                emailAccount.AddSmtpSettings(new Smtp(smtpPort, smtpHost));
            }
            try {
                await _emailClientAggregate.GetEmailsFromEmailAccountAsync(emailAccount, imapHost, imapPort);

                // await _emailAccountRepository.AddAsync (emailAccount);
            } catch {
                throw new Exception("Something went wrong during adding email account's datas");
            }
        }