public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model, string ReturnUrl) { try { Logger.Current.Informational("Are these ForgotPasswordViewModel fields valid : " + ModelState.IsValid); if (ModelState.IsValid) { ForgotPasswordViewModel forgotPasswordViewModel = model; var userName = forgotPasswordViewModel.Email + "|" + forgotPasswordViewModel.AccountId; var user = await UserManager.FindByNameAsync(userName); if (user != null) { int userID; int.TryParse(user.Id, out userID); string IP = Request.UserHostAddress; UserManager.InsertLoginAudit(userID, model.AccountId, IP, SignInActivity.ForgotPassword); Logger.Current.Informational("Based on details the UserId is:" + user.Id); string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); var body = ""; string filename = EmailTemplate.ForgotPassword.ToString() + ".txt"; string savedFileName = string.Empty; Logger.Current.Informational("The path for ForgotPassword Email template is :" + savedFileName); string imagesUrl = ConfigurationManager.AppSettings["IMAGE_HOSTING_SERVICE_URL"]; string accountLogo = string.Empty; string accountName = string.Empty; string subject = string.Empty; ApplicationServices.Messaging.Accounts.GetAccountImageStorageNameResponse response = accountService.GetStorageName(new ApplicationServices.Messaging.Accounts.GetAccountImageStorageNameRequest() { AccountId = model.AccountId }); if (response.AccountLogoInfo.SubscriptionId == 3) { string templateName = EmailTemplate.BDXForgotPassword.ToString() + ".txt"; savedFileName = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["EMAILTEMPLATES_PHYSICAL_PATH"].ToString(), templateName); subject = "BDX Lead Management - Password Reset Alert - " + response.AccountLogoInfo.AccountName + " : " + user.FirstName + " " + user.LastName; } else { savedFileName = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["EMAILTEMPLATES_PHYSICAL_PATH"].ToString(), filename); } if (!String.IsNullOrEmpty(response.AccountLogoInfo.StorageName)) { accountLogo = urlService.GetUrl(model.AccountId, ImageCategory.AccountLogo, response.AccountLogoInfo.StorageName); } else { accountLogo = ""; } accountName = response.AccountLogoInfo.AccountName; string accountImage = string.Empty; if (!string.IsNullOrEmpty(accountLogo)) { accountImage = accountImage + "<td align='right' valign='center' style='margin:0px;padding:0px 0px 25px 0px;'><img src='" + accountLogo + "' alt='" + accountName + "' style='width:100px;' width='100'> </td>"; } else { accountImage = ""; } if (response.AccountLogoInfo.SubscriptionId != 3) { subject = "SmartTouch Password Reset Alert - " + accountName + " : " + user.FirstName + " " + user.LastName; } var protectedUserEmail = MachineKey.Protect(GetBytes(user.Email.EmailId)); var securedUserEmail = Convert.ToBase64String(protectedUserEmail); var protectedAccountName = MachineKey.Protect(GetBytes(model.AccountName)); var securedAccountName = Convert.ToBase64String(protectedAccountName); string accountAddress = accountService.GetPrimaryAddress(new GetAddressRequest() { AccountId = model.AccountId }).Address; string accountPhoneNumber = accountService.GetPrimaryPhone(new GetPrimaryPhoneRequest() { AccountId = model.AccountId }).PrimaryPhone; using (StreamReader reader = new StreamReader(savedFileName)) { do { body = reader.ReadToEnd().Replace("[ACCOUNT]", Url.Encode(securedAccountName)).Replace("[GUID]", code).Replace("[FNAME]", user.FirstName).Replace("[LNAME]", user.LastName).Replace("[UNAME]", user.Email.EmailId).Replace("[USERID]", Url.Encode(securedUserEmail)).Replace("[STURL]", Request.Url.Authority.ToLower()).Replace("[ADDRESS]", accountAddress).Replace("[PHONE]", accountPhoneNumber).Replace("[IMAGES_URL]", imagesUrl).Replace("[AccountImage]", accountImage).Replace("[AccountName]", accountName); }while (!reader.EndOfStream); } Dictionary <Guid, string> providerDetails = accountService.GetTransactionalProviderDetails(model.AccountId); if (providerDetails.FirstOrDefault().Key != null) { await UserManager.SendEmailAsync(user.Id, subject + "|" + model.AccountPrimaryEmail + "|" + model.AccountId + "|" + providerDetails.FirstOrDefault().Key.ToString() + "|" + Request.Url.Host.ToLower(), body); } UserManager.UpdateResetFlag(userID, true); ViewBag.Message = "Email has been sent to you. Please check your Email."; return(View(ReturnUrl, model)); } if (user == null || !(await UserManager.ConfirmEmailAsync(user.Id, string.Empty)).Succeeded) { ModelState.AddModelError("", "The user either does not exist or is not confirmed."); return(View(ReturnUrl, model)); } } } catch (Exception ex) { ExceptionHandler.Current.HandleException(ex, DefaultExceptionPolicies.LOG_ONLY_POLICY); ModelState.AddModelError("", "[|An error occurred, please try again later.|]"); return(View(model)); } return(View(model)); }
private static string EmailBodyUserLevel(string accountLabel, string emailContent, int accountId) { string body = ""; string accountLogo = string.Empty; string accountAddress = accountService.GetPrimaryAddress(new GetAddressRequest() { AccountId = accountId }).Address; string accountPhoneNumber = accountService.GetPrimaryPhone(new GetPrimaryPhoneRequest() { AccountId = accountId }).PrimaryPhone; ApplicationServices.Messaging.Accounts.GetAccountImageStorageNameResponse response = accountService.GetStorageName(new ApplicationServices.Messaging.Accounts.GetAccountImageStorageNameRequest() { AccountId = accountId }); if (!String.IsNullOrEmpty(response.AccountLogoInfo.StorageName)) { accountLogo = urlService.GetUrl(accountId, ImageCategory.AccountLogo, response.AccountLogoInfo.StorageName); } else { accountLogo = ""; } string accountName = response.AccountLogoInfo.AccountName; string accountImage = string.Empty; if (!string.IsNullOrEmpty(accountLogo)) { accountImage = accountImage + "<td align='right' valign='center' style='margin:0px;padding:0px 0px 25px 0px;'><img src='" + accountLogo + "' alt='" + accountName + "' style ='width:100px;' width ='100'></td>"; } else { accountImage = ""; } string filename = EmailTemplate.WebVisitDailySummaryEmail.ToString() + ".txt"; string savedFileName = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["EMAILTEMPLATES_PHYSICAL_PATH"].ToString(), filename); using (StreamReader reader = new StreamReader(savedFileName)) { do { body = reader.ReadToEnd().Replace("[AccountName]", accountName).Replace("[AccountImage]", accountImage).Replace("[WebVisitData]", accountLabel + emailContent).Replace("[ADDRESS]", accountAddress).Replace("[PHONE]", accountPhoneNumber); } while (!reader.EndOfStream); } return(body); }
public ActionResult SendEmail(string userID, string accountID, string emailID, string name, string filename, string accountEmail, string resendInvite) { string code = UserManager.GenerateEmailConfirmationToken(userID); var body = ""; string savedFileName = string.Empty; string imagesUrl = ConfigurationManager.AppSettings["IMAGE_HOSTING_SERVICE_URL"]; int accountId = 0; int.TryParse(accountID, out accountId); string accountAddress = accountService.GetPrimaryAddress(new GetAddressRequest() { AccountId = accountId }).Address; string accountPhoneNumber = accountService.GetPrimaryPhone(new GetPrimaryPhoneRequest() { AccountId = accountId }).PrimaryPhone; string accountLogo = string.Empty; string accountName = string.Empty; string subject = string.Empty; ApplicationServices.Messaging.Accounts.GetAccountImageStorageNameResponse response = accountService.GetStorageName(new ApplicationServices.Messaging.Accounts.GetAccountImageStorageNameRequest() { AccountId = accountId }); if (response.AccountLogoInfo.SubscriptionId == 3) { string templateName = string.Empty; if (string.IsNullOrEmpty(resendInvite)) { templateName = EmailTemplate.BDXUserRegistration.ToString() + ".txt"; } else { templateName = EmailTemplate.BDXResendInvite.ToString() + ".txt"; } savedFileName = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["EMAILTEMPLATES_PHYSICAL_PATH"].ToString(), templateName); subject = "BDX Lead Management - New User Creation Notification – " + response.AccountLogoInfo.AccountName + " : " + name; } else { savedFileName = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["EMAILTEMPLATES_PHYSICAL_PATH"].ToString(), filename); } if (!String.IsNullOrEmpty(response.AccountLogoInfo.StorageName)) { accountLogo = urlService.GetUrl(accountId, ImageCategory.AccountLogo, response.AccountLogoInfo.StorageName); } else { accountLogo = ""; } accountName = response.AccountLogoInfo.AccountName; if (response.AccountLogoInfo.SubscriptionId != 3) { subject = "SmartTouch New User Creation Notification – " + accountName + " : " + name; } string accountImage = string.Empty; if (!string.IsNullOrEmpty(accountLogo)) { accountImage = accountImage + "<td align='right' valign='center' style='margin:0px;padding:0px 0px 25px 0px;'><img src='" + accountLogo + "' alt='" + accountName + "' style='width:100px;' width='100'> </td>"; } else { accountImage = ""; } using (StreamReader reader = new StreamReader(savedFileName)) { do { var protectedUserId = MachineKey.Protect(GetBytes(userID)); var protectedAccountId = MachineKey.Protect(GetBytes(accountID)); var protectedEmailId = MachineKey.Protect(GetBytes(emailID)); var securedUserID = Convert.ToBase64String(protectedUserId); var securedAccountID = Convert.ToBase64String(protectedAccountId); var securedEmailID = Convert.ToBase64String(protectedEmailId); var user = UserExtensions.ToFirstName(this.Identity) + " " + UserExtensions.ToLastName(this.Identity); body = reader.ReadToEnd().Replace("[USERNAME]", UserExtensions.ToUserName(this.Identity)).Replace("[ACCOUNTNAME]", accountName).Replace("[USER]", user).Replace("[ACCOUNTID]", Url.Encode(securedAccountID)).Replace("[NAME]", name).Replace("[EMAILID]", Url.Encode(securedEmailID)).Replace("[NEWUSEREMAIL]", emailID).Replace("[GUID]", code).Replace("[USERID]", Url.Encode(securedUserID)).Replace("[STURL]", Request.Url.Authority.ToLower()).Replace("[ADDRESS]", accountAddress).Replace("[PHONE]", accountPhoneNumber).Replace("[IMAGES_URL]", imagesUrl).Replace("[AccountImage]", accountImage); }while (!reader.EndOfStream); } var result = default(int); int.TryParse(accountID, out result); Dictionary <Guid, string> providerDetails = accountService.GetTransactionalProviderDetails(this.Identity.ToAccountID()); string fromEmail = string.IsNullOrEmpty(providerDetails.FirstOrDefault().Value) ? accountEmail : providerDetails.FirstOrDefault().Value; if (providerDetails.FirstOrDefault().Key != null) { UserManager.SendEmail(userID, subject + "|" + fromEmail + "|" + result + "|" + providerDetails.FirstOrDefault().Key + "|" + Request.Url.Authority.ToLower(), body); } return(Json(new { success = true, response = "[|Email has been sent to you Please check your Email.|]" }, JsonRequestBehavior.AllowGet)); }