public void CreateEmployee(string firstName, string lastName, string email) { if (string.IsNullOrEmpty(firstName)) { throw new ArgumentException("First name is required"); } if (string.IsNullOrEmpty(lastName)) { throw new ArgumentException("Last name is required"); } if (string.IsNullOrEmpty(email)) { throw new ArgumentException("Email is required"); } Employee newEmployee = new Employee(); newEmployee.FirstName = firstName; newEmployee.LastName = lastName; newEmployee.Email = email; var isCreated = SaveEmployee(newEmployee); if (isCreated) { _emailService.Notify(newEmployee); } }
public ActionResult SubmitContactRequestBusinessGenerator(ContactModel info) { var allCompanies = new CompanyRepository().GetAll(); var company = (from c in allCompanies where c.RowKey == info.ToCompanyID select c).Single(); if (!ModelState.IsValid) { if (Request.IsAjaxRequest()) { return(Json(new { status = "error", message = "All fields are required." })); } return(View(info)); } try { //Send email List <KeyValuePair <string, string> > fields = new List <KeyValuePair <string, string> >(); fields.Add(new KeyValuePair <string, string>("From Email", info.FromEmail)); fields.Add(new KeyValuePair <string, string>("Subject", info.Subject)); fields.Add(new KeyValuePair <string, string>("Quantity", info.Quantity)); fields.Add(new KeyValuePair <string, string>("Message", info.Message)); INotificationService notificationService = new EmailNotificationService( new EmailNotification(fields, company.SalesEmail, "Business Generator - Contact Request ", "*****@*****.**"), Properties.Settings.Default.SmtpHostName, Properties.Settings.Default.SmtpUserName, Properties.Settings.Default.SmtpPassword, true, Properties.Settings.Default.SmtpFromAddress); notificationService.Notify(); } catch (NotificationException) { ModelState.AddModelError("notifyerror", "Could not connect to mail server."); } if (Request.IsAjaxRequest()) { return(ModelState.IsValid ? Json(new { status = "Success", message = "Message sent successfully, we will contact you shortly." }) : Json(new { status = "error", message = "Could not connect to mail server." })); } return(ModelState.IsValid ? ContactRequestSuccess(info) : View(info)); }
public void Notify_Creates_Log_On_Failure() { // Arrange var logService = new Mock <ILogService>(); var emailService = new Mock <IEmailService>(); emailService.Setup(e => e.Send(It.IsAny <MailMessage>())).Throws(new SmtpException()); var user = new Mock <User>(); user.SetupProperty(u => u.Email, "*****@*****.**"); var template = new Mock <INotificationTemplate>(); template.Setup(t => t.Read()).Returns(new XElement("Email", new XElement("Subject"), new XElement("Body"))); template.Setup(t => t.ContainingDirectory).Returns(""); var templateObj = template.Object; var templateService = new Mock <ITemplateService>(); templateService.Setup(t => t.ParseTemplate(It.IsAny <string>(), templateObj, It.IsAny <object>())) .Returns(new Dictionary <string, string>() { { "Subject", "" }, { "Body", "" } }); // Parent email template templateService.Setup(t => t.ParseTemplate(It.IsAny <string>(), It.Is <INotificationTemplate>(n => n != templateObj), It.IsAny <object>())) .Returns(new Dictionary <string, string>() { { "Message", "" } }); var service = new EmailNotificationService(logService.Object, emailService.Object, templateService.Object, "*****@*****.**", "Support"); // Act service.Notify(user.Object, templateObj, null); // Assert logService.Verify(l => l.CreateLog(It.IsAny <Domain.Log>()), Times.Once()); }
public ActionResult ResetPassword(string email, string key = null) { if (key == null) { return(MenuView("MY PROFILE", "SubMenuFindAProduct", "None")); } else { if (email != null) { //TODO: Optimize query var user = new AzureUserRepository().GetAll().Where(u => u.Email.Equals(email, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(); //confirm that username exists if (user != null) { //Generate temporary password string pwdnew = Guid.NewGuid().ToString().Substring(0, 8); user.Password = pwdnew;//TODO: Hash password new AzureUserRepository().Save(user); //Send email List <KeyValuePair <string, string> > fields = new List <KeyValuePair <string, string> >(); fields.Add(new KeyValuePair <string, string>("Your new BRICS Business Generator Password Is: ", pwdnew));//TODO: improve security - make key dynamic INotificationService notificationService = new EmailNotificationService( new EmailNotification(fields, email, "Password Reset Request ", "*****@*****.**"),//TODO: place webmaster email in config file Properties.Settings.Default.SmtpHostName, Properties.Settings.Default.SmtpUserName, Properties.Settings.Default.SmtpPassword, true, Properties.Settings.Default.SmtpFromAddress); notificationService.Notify(); return(RedirectToAction("ResetPasswordComplete")); } else { throw new Exception("Specified user does not exist"); } } else { throw new Exception("Email address not specified"); } } }
public void Notify_Creates_Log_On_Failure() { // Arrange var logService = new Mock<ILogService>(); var emailService = new Mock<IEmailService>(); emailService.Setup(e => e.Send(It.IsAny<MailMessage>())).Throws(new SmtpException()); var user = new Mock<User>(); user.SetupProperty(u => u.Email, "*****@*****.**"); var template = new Mock<INotificationTemplate>(); template.Setup(t => t.Read()).Returns(new XElement("Email", new XElement("Subject"), new XElement("Body"))); template.Setup(t => t.ContainingDirectory).Returns(""); var templateObj = template.Object; var templateService = new Mock<ITemplateService>(); templateService.Setup(t => t.ParseTemplate(It.IsAny<string>(), templateObj, It.IsAny<object>())) .Returns(new Dictionary<string, string>() { {"Subject", ""}, {"Body", ""} }); // Parent email template templateService.Setup(t => t.ParseTemplate(It.IsAny<string>(), It.Is<INotificationTemplate>(n => n != templateObj), It.IsAny<object>())) .Returns(new Dictionary<string, string>() { {"Message", ""} }); var service = new EmailNotificationService(logService.Object, emailService.Object, templateService.Object, "*****@*****.**", "Support"); // Act service.Notify(user.Object, templateObj, null); // Assert logService.Verify(l => l.CreateLog(It.IsAny<Domain.Log>()), Times.Once()); }
public ActionResult SendResetPasswordEmail(string email) { var baseUrl = Settings.Default.BaseUrl; if (email != null) { //TODO: Optimize query var user = new AzureUserRepository().GetAll().Where(u => u.Email.Equals(email, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(); //confirm that username exists if (user != null) { //Send email List <KeyValuePair <string, string> > fields = new List <KeyValuePair <string, string> >(); string resetUrl = baseUrl + "account/resetpassword?key=86BE6298F3D3&email=" + Url.Encode(email); fields.Add(new KeyValuePair <string, string>("A Password Reset Request Was Received. If you requested this please click the following link: <a href=\"%url%\"/>".Replace("%url%", resetUrl), resetUrl));//TODO: improve security - make key dynamic INotificationService notificationService = new EmailNotificationService( new EmailNotification(fields, email, "Password Reset Request ", "*****@*****.**"),//TODO: place webmaster email in config file Properties.Settings.Default.SmtpHostName, Properties.Settings.Default.SmtpUserName, Properties.Settings.Default.SmtpPassword, true, Properties.Settings.Default.SmtpFromAddress); notificationService.Notify(); } else { throw new Exception("Specified user does not exist"); } } else { throw new Exception("Email address not specified"); } return(MenuView("MY PROFILE", "SubMenuFindAProduct", "None")); }