public async Task SendWelcomeEmailAndStoreKey(string userId, string keyDetail, CancellationToken cancellationToken) { Guard.Argument(userId, nameof(userId)).NotNull().NotEmpty().NotWhiteSpace(); Guard.Argument(keyDetail, nameof(keyDetail)).NotNull().NotEmpty().NotWhiteSpace(); var user = await _userRepository.GetUserById(userId, cancellationToken); if (user == null) { throw new UserException($"User does not exists '{userId}'. Cannot send the welcome email with key as attachment"); } var userKey = await _keyRepository.GetByUserId(userId, cancellationToken); if (userKey != null) { _logger.Warning($"Key already recorded for an '{userId}'"); throw new UserException($"Key already recorded for an '{userId}'"); } await _keyRepository.CreateKey( new KeyDao { Id = Guid.NewGuid().ToString(), UserId = userId, KeyDetails = keyDetail }, cancellationToken); var emailBody = _emailTemplateHelper.GetWelcomeEmailBody(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), user.FirstName); var emailMessage = new EmailDao { ToAddress = user.Email, ToName = user.FullName, FromAddress = Constants.AutomatedEmailFromAddress, Subject = Constants.WelcomeEmailSubject, HtmlBody = emailBody, }; await _emailService.SendEmail(emailMessage, cancellationToken); _logger.Information($"Welcome email sent for user '{user.Id}'"); }