private void SendEmail(CustomUser customUser, string relayState = null) { logger.Debug("enter send email from Registration"); EmailMgmt emailService = new EmailMgmt( appSettings["SMTPHost"], int.Parse(appSettings["SMTPPort"]), appSettings["EmailSSLEnable"].Equals("true"), appSettings["FromEmailAddress"], appSettings["FromEmailPassword"] ); try { //var fromEmailAddress = CloudConfigurationManager.GetSetting("ReplyToEmailAddress"); //var fromEmailDisplayName = CloudConfigurationManager.GetSetting("FromEmailDisplayName"); var fromEmailAddress = appSettings["ReplyToEmailAddress"]; var fromEmailDisplayName = appSettings["FromEmailDisplayName"]; var toEmailAddress = customUser.Profile.Email; var activateUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/Registration/Activate?token=" + TempData["recoveryToken"] + "&locator=" + TempData["oktaId"]; if (!string.IsNullOrEmpty(relayState)) { activateUrl += "&relayState=" + relayState; } ViewData["activateUrl"] = activateUrl; string body = oktaApiHelper.RenderPartialToString(this.ControllerContext, "_AccountActivationEmail", ViewData, TempData); logger.Debug("SendEmail " + toEmailAddress + " message " + body); emailService.SendEmail(fromEmailAddress, fromEmailDisplayName, toEmailAddress, "Account Activation Request", body, true); } catch (Exception ex) { logger.Error("Mail send failed to " + customUser.Profile.Email + " error; " + ex.Message); TempData["errMessage"] = "We could not send an email to " + customUser.Profile.Email + ". If this error persists, please contact the help desk via the contact information at the bottom of the page."; } }
private void SendEmail(CustomUser customUser, string relayState = null) { logger.Debug("enter send email from ForgotPassword"); EmailMgmt emailService = new EmailMgmt( appSettings["SMTPHost"], int.Parse(appSettings["SMTPPort"]), appSettings["EmailSSLEnable"].Equals("true"), appSettings["FromEmailAddress"], appSettings["FromEmailPassword"] ); try { //var fromEmailAddress = CloudConfigurationManager.GetSetting("ReplyToEmailAddress"); //var fromEmailDisplayName = CloudConfigurationManager.GetSetting("FromEmailDisplayName"); var fromEmailAddress = appSettings["ReplyToEmailAddress"]; var fromEmailDisplayName = appSettings["FromEmailDisplayName"]; var toEmailAddress = customUser.Profile.Email; var activateUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/Registration/Activate?token=" + TempData["recoveryToken"] + "&locator=" + TempData["oktaId"]; if (!string.IsNullOrEmpty(relayState)) { activateUrl += "&relayState=" + relayState; } ViewData["activateUrl"] = activateUrl; string body = oktaApiHelper.RenderPartialToString(this.ControllerContext, "_ForgotUsernameEmail", ViewData, TempData); logger.Debug("SendEmail " + toEmailAddress + " message " + body); emailService.SendEmail(fromEmailAddress, fromEmailDisplayName, toEmailAddress, "Account Activation Request", body, true); } catch (Exception ex) { logger.Error("Mail send failed to " + customUser.Profile.Email + " error; " + ex.Message); TempData["errMessage"] = "We could not send an email to " + customUser.Profile.Email + ". If this error persists, please contact the help desk via the contact information at the bottom of the page."; } try { var fromEmailAddress = appSettings["FromEmailAddress"].ToString(); var fromEmailDisplayName = appSettings["FromEmailDisplayName"].ToString(); var fromEmailPassword = appSettings["FromEmailPassword"].ToString(); var smtpHost = appSettings["SMTPHost"].ToString(); var smtpPort = appSettings["SMTPPort"].ToString(); var emailSSLEnable = appSettings["EmailSSLEnable"].ToString(); var toEmailAddress = customUser.Profile.Email; var toEmailDisplayName = customUser.Profile.LastName + ", " + customUser.Profile.FirstName; string body = oktaApiHelper.RenderPartialToString(this.ControllerContext, "_ForgotUsernameEmail", ViewData, TempData); MailMessage message = new MailMessage(); message.From = new MailAddress(fromEmailAddress, fromEmailDisplayName); message.To.Add(toEmailAddress); //new MailAddress(toEmailAddress, toEmailDisplayName)); message.Subject = "Forgotten Password Reset Request"; message.IsBodyHtml = true; message.Body = body; var client = new SmtpClient(); client.Credentials = new NetworkCredential(fromEmailAddress, fromEmailPassword); client.Host = smtpHost; if (emailSSLEnable == "true") { client.EnableSsl = true; } else { client.EnableSsl = false; } client.Port = !string.IsNullOrEmpty(smtpPort) ? Convert.ToInt32(smtpPort) : 0; logger.Debug("SendEmail " + toEmailAddress + " message " + message.Body.ToString()); client.Send(message); } catch (Exception ex) { logger.Error("Mail send failed to " + customUser.Profile.Email + " error; " + ex.Message); TempData["errMessage"] = "Mail send failed to " + customUser.Profile.Email + " error; " + ex.Message; // throw (new Exception("Mail send failed to " + oktaUserObj.Profile.Email + ", though password change was done." + ex.Message)); } }