public async Task <ActionResult> SendEmail(EmailFormVM vm) { // TODO: Use dropdown for contacts to determine recipient (enum.chosen.value.tostring())s if (ModelState.IsValid) { var body = "<p>Email Fra: {0} ({1})</p><p>Besked:</p><p>{2}</p>"; var message = new MailMessage(); message.To.Add(new MailAddress("*****@*****.**")); message.From = new MailAddress("*****@*****.**"); message.Subject = "Kontaktformular"; message.Body = string.Format(body, vm.FromName, vm.FromEmail, vm.Message); message.IsBodyHtml = true; using (var smtp = new SmtpClient()) { var credential = new NetworkCredential { UserName = "******", Password = "******" }; smtp.Credentials = credential; smtp.Host = "smtp.unoeuro.com"; smtp.Port = 587; smtp.EnableSsl = true; try { await smtp.SendMailAsync(message); } catch (Exception e) { ViewBag.ErrorMessage = "Vi havde problemer med at sende din besked. Prøv venligst igen, eller send til en anden modtager."; return(View("Error")); } ViewBag.EmailSuccesful = true; ModelState.Clear(); return(View("Contact", new EmailFormVM())); } } return(View("Contact", vm)); }
public ActionResult EmailForm(EmailFormVM model) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; if (!ModelState.IsValid) { return(View(model)); } if (Request.QueryString.AllKeys.Contains("s") && Request.QueryString.AllKeys.Contains("t")) { string siteIdentifier = Request.QueryString.GetValues("s").FirstOrDefault(); var referrer = ReferrersManagers.GetReferrer(siteIdentifier); int ncid = GetNcId(); int visitId = GetVisitId(); string email = model.Email; string token = Request.QueryString.GetValues("t").FirstOrDefault(); CustomerDT customer = null; try { customer = CustomersManager.NewCustomer(((SiteDT)ViewBag.Site), email, token, ncid, visitId, referrer.GetDT()); ViewBag.Customer = customer; } catch (Exception ex) { SendExceptionEmail(ex.ToString()); } if (customer != null) { return(View("Welcome")); } else { return(View("Error")); } } throw new Exception(); }
[HttpPost]// post type of method follows this public async Task <ActionResult> ForgotPassword(EmailFormVM emailFormVM) { try { HttpRequest request = HttpContext.Request; string token = this.RandomString(10); byte[] tmpToken = ASCIIEncoding.ASCII.GetBytes(token); byte[] tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpToken); string newHashedToken = ByteArrayToString(tmpHash); NewWebSubContext context = HttpContext.RequestServices.GetService(typeof(new_websub.NewWebSubContext)) as NewWebSubContext; using (MySqlConnection conn = context.GetConnection()) { conn.Open(); string query = "select * from useraccounts where Email=@Email"; MySqlCommand cmd = new MySqlCommand(query, conn); MySqlParameter param = new MySqlParameter("@Email", emailFormVM.toEmail); param.MySqlDbType = MySqlDbType.VarChar; cmd.Parameters.Add(param); MySqlDataReader reader = cmd.ExecuteReader(); if (!reader.Read()) { ViewBag.message = "This email is not exist."; return(View()); } reader.Close(); // insert hashed_token inside the database try { query = "Update useraccounts set Hashed_Token=@token WHERE Email=@Email"; cmd = new MySqlCommand(query, conn); param = new MySqlParameter("@token", newHashedToken); param.MySqlDbType = MySqlDbType.VarChar; cmd.Parameters.Add(param); param = new MySqlParameter("@Email", emailFormVM.toEmail); param.MySqlDbType = MySqlDbType.VarChar; cmd.Parameters.Add(param); cmd.ExecuteNonQuery(); // mail sent string redirectLinnk = request.Host.ToString() + "/Account/ResetPassword?email=" + emailFormVM.toEmail + "&token=" + token; MailMessage mail = new MailMessage(); mail.From = new MailAddress(_emailSettings.Value.PrimaryEmail); mail.Subject = "Reset password"; mail.IsBodyHtml = true; mail.Body = "<p>Please click following url to reset your password <a href='" + redirectLinnk + "'>" + redirectLinnk + "</a></p>"; mail.Sender = new MailAddress(_emailSettings.Value.PrimaryEmail); mail.IsBodyHtml = true; mail.To.Add(emailFormVM.toEmail); SmtpClient smtp = new SmtpClient(); smtp.Host = _emailSettings.Value.PrimaryDomain; //Or Your SMTP Server Address smtp.Port = _emailSettings.Value.PrimaryPort; smtp.UseDefaultCredentials = false; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Credentials = new System.Net.NetworkCredential(_emailSettings.Value.PrimaryEmail, _emailSettings.Value.PrimaryPassword); //Or your Smtp Email ID and Password smtp.EnableSsl = _emailSettings.Value.EnableSsl; smtp.Send(mail); // should be removed for local testing ViewBag.message = "Recovery Email has been sent, please check your mail box."; // ViewBag.message = redirectLinnk; // for only testing in local } catch (Exception ex1) { ViewBag.message = ex1.Message; } } } catch (Exception ex) { ViewBag.message = ex.Message; } return(View()); }