public async Task <EmailTemplate> GetConfirmAccountEmailAsync(ConfirmAccountEmailViewModel confirmAccountEmailViewModel, CultureInfo cultureInfo) { CultureInfo cultureInfoBackup = CultureInfo.CurrentCulture; CultureInfo.CurrentCulture = cultureInfo; CultureInfo.CurrentUICulture = cultureInfo; string view = GetConfirmAccountEmailView(cultureInfo); if (string.IsNullOrEmpty(view)) { throw new Exception("Confirm account email view model path is empty"); } string subject = GetConfirmAccountEmailSubject(cultureInfo); if (string.IsNullOrEmpty(subject)) { throw new Exception("Confirm account email subject is empty"); } string body = await _templateRenderer.RenderViewAsync(view, confirmAccountEmailViewModel).ConfigureAwait(false); CultureInfo.CurrentCulture = cultureInfoBackup; CultureInfo.CurrentUICulture = cultureInfoBackup; return(new EmailTemplate(subject, body)); }
public async Task SendConfirmEmail(string email, string baseUrl) { var confirmAccountModel = new ConfirmAccountEmailViewModel($"{baseUrl}"); string body = await razorViewToStringRenderer .RenderViewToStringAsync( "~/Views/Emails/ConfirmAccount/ConfirmAccountEmail.cshtml", confirmAccountModel); SendEmailAsync(email, "Confirm your Account", body); }
public async Task <IActionResult> ConfirmAccountEmailText() { var confirmAccountEmailViewModel = new ConfirmAccountEmailViewModel(true, "https://www.google.com"); var content = await _razorViewToStringRenderer.RenderViewToStringAsync("/Views/Emails/Text/Account/ConfirmEmail.cshtml", confirmAccountEmailViewModel); return(new ContentResult { ContentType = "text/html", StatusCode = (int)HttpStatusCode.OK, Content = content }); }
public async Task Register(string email, string baseUrl) { var confirmAccountModel = new ConfirmAccountEmailViewModel($"{baseUrl}/{Guid.NewGuid()}"); string body = await RazorTemplateEngine.RenderAsync("/Views/Emails/ConfirmAccount/ConfirmAccountEmail.cshtml", confirmAccountModel); var toAddresses = new List <string> { email }; SendEmail(toAddresses, "*****@*****.**", "Confirm your Account", body); }
public async Task Register(string email, string baseUrl) { // TODO: Validation + actually add the User to a DB + whatever else // TODO: Base URL off of ASP.NET Core Identity's logic or some other mechanism, rather than hardcoding to creating a random guid var confirmAccountModel = new ConfirmAccountEmailViewModel($"{baseUrl}/{Guid.NewGuid()}"); string body = await _razorViewToStringRenderer.RenderViewToStringAsync("/Views/Emails/ConfirmAccount/ConfirmAccountEmail.cshtml", confirmAccountModel); var toAddresses = new List <string> { email }; SendEmail(toAddresses, "*****@*****.**", "Confirm your Account", body); }
public async Task Handle(UserCreatedEvent @event, CancellationToken cancellationToken) { var confirmAccountModel = new ConfirmAccountEmailViewModel { Token = @event.Token, Username = @event.Username, UserId = @event.UserId }; string body = await _razorViewToStringRenderer.RenderViewToStringAsync("/Views/ConfirmAccountEmail.cshtml", confirmAccountModel); await _notification.SendEmailAsync(new MailMessage { Body = body, From = new MailAddress("*****@*****.**"), To = { new MailAddress(@event.Email) }, Priority = MailPriority.High, Subject = $"Account confirmation for user {@event.Username}", IsBodyHtml = true }); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new ExpenseManagerUser { FirstName = Input.FirstName, LastName = Input.LastName, DOB = Input.DOB, UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); var webRoot = _env.WebRootPath; //get wwwroot Folder //Get TemplateFile located at wwwroot/Templates/EmailTemplate/Register_EmailTemplate.html var pathToFile = webRoot + Path.DirectorySeparatorChar.ToString() + "Templates" + Path.DirectorySeparatorChar.ToString() + "EmailTemplate" + Path.DirectorySeparatorChar.ToString() + "Welcome_EmailTemplate.html"; var subject = "Confirm your email"; var builder = new StringBuilder(); // BodyBuilder(); using (var reader = System.IO.File.OpenText(pathToFile)) { builder.Append(reader.ReadToEnd()); builder.Replace("{0}", Input.FirstName); builder.Replace("{1}", callbackUrl); } string messageBody = builder.ToString(); //var builder = new BodyBuilder(); //using (var reader = System.IO.File.OpenText(pathToFile)) //{ // builder.HtmlBody = reader.ReadToEnd(); //} //string messageBody = string.Format(builder.HtmlBody, //Input.FirstName //); var confirmAccountModel = new ConfirmAccountEmailViewModel(callbackUrl, Input.FirstName); string body = await _razorViewToStringRenderer.RenderViewToStringAsync("/Views/Emails/ConfirmAccount/ConfirmAccountEmail.cshtml", confirmAccountModel); await _emailSender.SendEmailAsync(Input.Email, subject, body); //$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); //await _signInManager.SignInAsync(user, isPersistent: false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }