public ActionResult Contact(string subject) { Models.Home.ContactEmailFormViewModel model = new Models.Home.ContactEmailFormViewModel { Subject = subject }; ViewBag.Message = "Your contact form."; return(View(model)); }
public async Task <ActionResult> Contact(Models.Home.ContactEmailFormViewModel model) { if (ModelState.IsValid) { string userRole = ""; if (User.IsInRole(Role.Admin.ToString())) { userRole = Role.Admin.ToString(); } else if (User.IsInRole(Role.IdeaCarrier.ToString())) { userRole = Role.IdeaCarrier.ToString(); } /*...*/ var smtpClients = db.SmtpClients.ToList(); foreach (Models.SMTP.SmtpClient smtpClient in smtpClients) { if (smtpClient.Active) { try { MailMessage message = new MailMessage { //Sender = new MailAddress(User.Identity.Name), From = new MailAddress(User.Identity.Name), Subject = model.Subject, Body = $"\nAvsändare:\n{User.Identity.Name} ({userRole})\n{User.Identity.GetUserId()}\n----------------------------------------\n{model.Message}", //string.Format(bodyFormat, User.Identity.Name, userRole, User.Identity.GetUserId(), model.Message), IsBodyHtml = false }; var Admins = db.Users.Where(u => u.Roles.Where(r1 => r1.RoleId == db.Roles.Where(r2 => r2.Name == Role.Admin.ToString()).FirstOrDefault().Id).Any()).ToList(); foreach (var admin in Admins) { message.To.Add(admin.Email); } using (var smtp = new SmtpClient()) { var credential = new NetworkCredential { UserName = smtpClient.CredentialUserName, Password = smtpClient.CredentialPassword }; smtp.Credentials = credential; smtp.Host = smtpClient.Host; smtp.Port = smtpClient.Port; smtp.EnableSsl = smtpClient.EnableSsl; await smtp.SendMailAsync(message); return(RedirectToAction("MessageSent", new { sent = true })); } } catch { //return RedirectToAction("MessageSent", new { sent = false }); } } } return(RedirectToAction("MessageSent", new { sent = false })); } return(View(model)); }