public async Task <List <string> > ResetPasswordAsync(string email) { var user = await _userManager.FindByEmailAsync(email); var errors = new List <string>(); if (user is null) { errors.Add(USER_NOT_FOUND_ERROR); return(errors); } var isEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user); if (!isEmailConfirmed) { errors.Add(EMAIL_IS_NOT_CONFIRMED_ERROR); return(errors); } var token = await _userManager.GeneratePasswordResetTokenAsync(user); var newPassword = PasswordGenerator.GeneratePassword(); var result = await _userManager.ResetPasswordAsync(user, token, newPassword); if (!result.Succeeded) { foreach (var error in result.Errors) { errors.Add(error.Description); } return(errors); } var subject = RESET_PASSWORD_SUBJECT; var body = $"{RESET_PASSWORD_BODY} {newPassword}"; await _emailProvider.SendAsync(email, subject, body); return(errors); }
public async Task SimpleEmailServiceProvider_Should_Return_MessageId_If_Response_Is_Accepted() { //Arrange var from = "*****@*****.**"; var to = "*****@*****.**"; var subject = "testing"; var body = "body not found"; var response = new SendEmailResponse { MessageId = "TestId" }; amazonSimpleEmailService.Setup(a => a.SendEmailAsync(It.IsAny <SendEmailRequest>(), It.IsAny <CancellationToken>())) .ReturnsAsync(response); //Act var result = await simpleEmailServiceProvider.SendAsync(from, to, subject, body); //Assert Assert.IsNotNull(result); Assert.AreEqual(response.MessageId, result); }
public async Task SendEmail(SendEmailRequest request) { var postResponse = await _postService.GetPostAsync(new GetPostRequest() { Id = request.PostId }); if (!postResponse.IsSuccess) { throw new Exception("Post is not found"); } var userResponse = await _accountService.GetUserAsync(new GetUserRequest() { UserId = request.UserId }); if (userResponse.IsSuccess) { await _emailProvider.SendAsync(userResponse.User.EmailAddress, "Kindle E-Book", null, postResponse.Post.BlobUrl); } }
/// <summary> /// Execute send emails /// </summary> /// <param name="sendOptions">Email send options</param> /// <returns>Return the email send results</returns> internal static async Task <List <SendEmailResult> > ExecuteSendAsync(IEnumerable <SendEmailOptions> sendOptions) { if (sendOptions.IsNullOrEmpty()) { return(new List <SendEmailResult>(0)); } if (EmailProvider == null) { throw new EZNEWException("No mail provider is configured"); } Dictionary <string, List <SendEmailOptions> > emailInfoGroups = new Dictionary <string, List <SendEmailOptions> >(); Dictionary <string, EmailAccount> accounts = new Dictionary <string, EmailAccount>(); #region Gets email account foreach (var sendInfo in sendOptions) { var account = GetAccount(sendInfo); if (account == null) { continue; } string accountKey = account.IdentityKey; if (UseSameEmailAccount) { emailInfoGroups[accountKey] = sendOptions.ToList(); accounts[accountKey] = account; break; } if (accounts.ContainsKey(accountKey)) { emailInfoGroups[accountKey].Add(sendInfo); } else { emailInfoGroups.Add(accountKey, new List <SendEmailOptions>() { sendInfo }); accounts.Add(accountKey, account); } } #endregion #region Execute send IEnumerable <SendEmailResult> sendResults = null; //Single email account if (emailInfoGroups.Count == 1) { var firstGroup = emailInfoGroups.First(); var account = accounts[firstGroup.Key]; sendResults = await EmailProvider.SendAsync(account, firstGroup.Value.ToArray()); } else { //Multiple email account var emailTasks = new Task <List <SendEmailResult> > [emailInfoGroups.Count]; var groupIndex = 0; foreach (var optionGroup in emailInfoGroups) { var account = accounts[optionGroup.Key]; emailTasks[groupIndex] = EmailProvider.SendAsync(account, optionGroup.Value.ToArray()); groupIndex++; } sendResults = (await Task.WhenAll(emailTasks).ConfigureAwait(false)).SelectMany(c => c); } #endregion //callback ThreadPool.QueueUserWorkItem(s => { EmailSentCallback?.Invoke(sendResults?.Select(c => c.Clone()).ToList() ?? new List <SendEmailResult>(0)); }); return(sendResults.ToList()); }
public async Task <bool> SendAsync(EmailMessage message) { message.From ??= _options.Value.From; return(await _provider.SendAsync(message)); }