public async Task <IActionResult> ForgotPassword(ForgotPassword_vmodel model) { if (ModelState.IsValid) { var user = await userManager.FindByEmailAsync(model.Email); if (user != null && await userManager.IsEmailConfirmedAsync(user)) { var token = await userManager.GeneratePasswordResetTokenAsync(user); var passwordResetLink = Url.Action("ResetPassword", "Account", new { email = model.Email, token }, Request.Scheme); string message = HtmlEmailTemplate.CreateHtmlBody(user.UserName, "Has click para restablecer su contraseña:", passwordResetLink, "Verificar Correo Electrónico"); mailService.SendEmail(user.Email, "Restablecer su contraseña", message); logger.Log(LogLevel.Information, $"Se envio el token de restablecimiento de contraseña para el usuario {user.UserName}"); return(View("ForgotPasswordConfirmation")); } return(View("ForgotPasswordConfirmation")); } return(View(model)); }
/// <summary> /// /// </summary> /// <param name="template"></param> /// <param name="parameters"></param> /// <returns></returns> protected HtmlEmail ProcessTemplate(HtmlEmailTemplate template, Dictionary<String, String> parameters) { HtmlEmail processedTemplate = new HtmlEmail(); processedTemplate.EmailSubject = ReplaceFields(template.Subject, parameters); processedTemplate.EmailText = ReplaceFields(template.Body, parameters); processedTemplate.FromAddress = template.FromAddress; processedTemplate.FromName = template.FromName; return processedTemplate; }
public async Task <IActionResult> Register(CreateAccount_vmodel model) { if (ModelState.IsValid) { if (authorizedEmailRepo.GetByEmail(model.Email) == null) { ModelState.AddModelError(string.Empty, "Su correo NO esta autorizado para registrarse"); return(View(model)); } var user = new App_IdentityUser() { UserName = model.Username, Email = model.Email, Gender = model.Gender }; var result = await userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var token = await userManager.GenerateEmailConfirmationTokenAsync(user); var confirmationLink = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, token }, Request.Scheme); string message = HtmlEmailTemplate.CreateHtmlBody(user.UserName, "Has click para verificar su cuenta de correo electrónico:", confirmationLink, "Verificar Correo Electrónico"); mailService.SendEmail(user.Email, "Verificación de correo electrónico", message); logger.Log(LogLevel.Information, $"Se envio el token de confirmacion de email para el usuario {user.UserName}"); if (signInManager.IsSignedIn(User)) { return(RedirectToAction("ListUsers", "Administration")); } await signInManager.SignInAsync(user, isPersistent : false); if ((await userManager.GetClaimsAsync(user)).Count() > 0) { return(RedirectToAction("Index", "Home")); } return(RedirectToAction("WithoutClaims", new { emailConfirmed = user.EmailConfirmed })); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } return(View(model)); }
public void EmailTemplate_HtmlCode_01() { // Arrange var template = new HtmlEmailTemplate(); template.Load("{{html for(int i = 0; i < 3; i++) { }}{{#i}} {{ } }}"); // Act var result = template.Run(out HtmlEmailMetadata metadata); // Assert Assert.AreEqual("0 1 2 ", result); }
public void EmailTemplate_HtmlAttributeEncode_01() { // Arrange var template = new HtmlEmailTemplate(); template.Load("Hello <a href=\"{{#attr \"Sample&Sample\"}}\">Meziantou</a>!"); // Act var result = template.Run(out HtmlEmailMetadata metadata); // Assert Assert.AreEqual("Hello <a href=\"Sample&Sample\">Meziantou</a>!", result); }
public void EmailTemplate_UrlEncode_01() { // Arrange var template = new HtmlEmailTemplate(); template.Load("Hello <a href=\"http://www.localhost.com/{{#url \"Sample&Url\" }}\">Meziantou</a>!"); // Act var result = template.Run(out HtmlEmailMetadata metadata); // Assert Assert.AreEqual("Hello <a href=\"http://www.localhost.com/Sample%26Url\">Meziantou</a>!", result); }
public void EmailTemplate_HtmlEncode_01() { // Arrange var template = new HtmlEmailTemplate(); template.Load("Hello {{#html \"<Meziantou>\" }}!"); // Act var result = template.Run(out HtmlEmailMetadata metadata); // Assert Assert.AreEqual("Hello <Meziantou>!", result); }
public void EmailTemplate_01() { // Arrange var template = new HtmlEmailTemplate(); template.Load("Hello {{# \"Meziantou\" }}!"); // Act var result = template.Run(out HtmlEmailMetadata metadata); // Assert Assert.AreEqual("Hello Meziantou!", result); Assert.AreEqual(null, metadata.Title); }
public void EmailTemplate_Section_01() { // Arrange var template = new HtmlEmailTemplate(); template.Load("Hello {{@begin section title}}{{# \"Meziantou\" }}{{@end section}}!"); // Act var result = template.Run(out HtmlEmailMetadata metadata); // Assert Assert.AreEqual("Hello Meziantou!", result); Assert.AreEqual("Meziantou", metadata.Title); }
public void NAME() { // Build the container. var container = Setup(); var html = new HtmlEmailTemplate(); html.Name = "ReviewerAssigned"; html.Content = "test"; var repo = new HtmlEmailTemplateRepository(container.Resolve <IDocumentStore>()); repo.Save(html); }
public void EmailTemplate_Cid_01() { // Arrange var template = new HtmlEmailTemplate(); template.Load("<img src=\"{{cid test1.png}}\" /><img src=\"{{cid test2.png}}\" />"); // Act var result = template.Run(out HtmlEmailMetadata metadata); // Assert Assert.AreEqual("<img src=\"cid:test1.png\" /><img src=\"cid:test2.png\" />", result); Assert.AreEqual(2, metadata.ContentIdentifiers.Count); Assert.AreEqual("test1.png", metadata.ContentIdentifiers[0]); Assert.AreEqual("test2.png", metadata.ContentIdentifiers[1]); }
public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null) { returnUrl ??= Url.Content("~/"); SignIn_vmodel model = new SignIn_vmodel() { ReturnUrl = returnUrl, ExternalLogins = (await signInManager.GetExternalAuthenticationSchemesAsync()).ToList() }; if (remoteError != null) { ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}"); return(View("Login", model)); } var info = await signInManager.GetExternalLoginInfoAsync(); if (info == null) { ModelState.AddModelError(string.Empty, "Error loading external login information"); return(View("Login", model)); } var email = info.Principal.FindFirstValue(ClaimTypes.Email); if (authorizedEmailRepo.GetByEmail(email) == null) { ModelState.AddModelError(string.Empty, "Su correo NO esta autorizado para registrarse"); return(View("Login", model)); } App_IdentityUser user; if (email != null) { user = await userManager.FindByEmailAsync(email); if (user != null && !user.EmailConfirmed) { ModelState.AddModelError(string.Empty, "Email no confirmado aún"); return(View("Login", model)); } } var signInResult = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true); if (signInResult.Succeeded) { user = await userManager.FindByEmailAsync(info.Principal.FindFirstValue(ClaimTypes.Email)); if ((await userManager.GetClaimsAsync(user)).Count() > 0) { return(LocalRedirect(returnUrl)); } return(RedirectToAction("WithoutClaims", new { emailConfirmed = user.EmailConfirmed })); } else { user = await userManager.FindByEmailAsync(email); if (user == null) { string username = info.Principal.FindFirstValue(ClaimTypes.Name); if (username.Contains(" ")) { username = username.Split(' ')[0].ToLower(); } else { username = username.ToLower(); } user = new App_IdentityUser() { UserName = username, Email = info.Principal.FindFirstValue(ClaimTypes.Email) }; var result = await userManager.CreateAsync(user); if (!result.Succeeded) { foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, $"{error.Description}"); } return(View("Login", model)); } var token = await userManager.GenerateEmailConfirmationTokenAsync(user); var confirmationLink = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, token }, Request.Scheme); string message = HtmlEmailTemplate.CreateHtmlBody(user.UserName, "Has click para verificar su cuenta de correo electrónico:", confirmationLink, "Verificar Correo Electrónico"); mailService.SendEmail(user.Email, "Verificación de correo electrónico", message); } await userManager.AddLoginAsync(user, info); await signInManager.SignInAsync(user, isPersistent : false); if ((await userManager.GetClaimsAsync(user)).Count() > 0) { return(LocalRedirect(returnUrl)); } return(RedirectToAction("WithoutClaims", new { emailConfirmed = user.EmailConfirmed })); } }
public DomingoBlError SendEmailWithAttachments(string emailAlias, List<MailAddress> deliveryAddresses, Dictionary<String, String> parameters, string attachmentPath) { try { using (TravelogyDevEntities1 context = new TravelogyDevEntities1()) { // get the xml template from DB HtmlEmailTemplate emailTemplate = context.HtmlEmailTemplates.Where(p => p.Alias == emailAlias).FirstOrDefault(); if (emailTemplate == null) { return new DomingoBlError() { ErrorCode = 200, ErrorMessage = "Invalid template alias" }; } // replace the parameters into the template HtmlEmail processedTemplate = ProcessTemplate(emailTemplate, parameters); MailMessage objMessage = new MailMessage(); objMessage.From = new MailAddress(processedTemplate.FromAddress, processedTemplate.FromName); foreach (MailAddress address in deliveryAddresses) { processedTemplate.ToAddress = address.Address; // construct the SMTP mail and send objMessage.From = new MailAddress(processedTemplate.FromAddress, processedTemplate.FromName); objMessage.Subject = processedTemplate.EmailSubject; objMessage.IsBodyHtml = true; objMessage.Body = processedTemplate.EmailText; objMessage.To.Clear(); objMessage.To.Add(address); var sendinBlue = new API("ICdw29ZamvD0WXcJ"); // sendinblue access key from the portal Dictionary<string, Object> data = new Dictionary<string, Object>(); Dictionary<string, string> to = new Dictionary<string, string>(); to.Add(address.Address, address.DisplayName); List<string> from_name = new List<string>(); from_name.Add(processedTemplate.FromAddress); from_name.Add(processedTemplate.FromName); List<string> attachment = new List<string>(); attachment.Add(attachmentPath); data.Add("to", to); data.Add("from", from_name); data.Add("subject", processedTemplate.EmailSubject); data.Add("html", processedTemplate.EmailText); data.Add("attachment", attachment); Object sendEmail = sendinBlue.send_email(data); Console.WriteLine(sendEmail); // save the mail to DB // to save the mail on DB context.HtmlEmails.Add(processedTemplate); context.SaveChanges(); } } return new DomingoBlError() { ErrorCode = 0, ErrorMessage = "" }; } // exception part catch (FormatException ex) { return new DomingoBlError() { ErrorCode = 100, ErrorMessage = string.Format("Error : {0}", ex) }; } }