I try to create a contact form in mvc 4, it's work but the sender (email and name) is always the same that I use in my credentials for the smtp, I want to display the email and the name of the user who send it from my contact form, any help? this is my code:
model:
[Required(ErrorMessage="Requerido")]
public string nombre { get; set; }
[Required(ErrorMessage = "Requerido")]
[DataType(DataType.EmailAddress)]
public string email { get; set; }
[Required(ErrorMessage = "Requerido")]
public string telefono { get; set; }
[Required(ErrorMessage = "Requerido")]
public string comentario { get; set; }
controller:
[HttpPost]
public ActionResult Index(contactModel model)
{
if (ModelState.IsValid)
{
insertContact(model.nombre, model.email, model.telefono, model.comentario);
TempData["notice"] = "your form was submitted.";
return RedirectToAction("Index", "Home");
}
return View();
}
private void insertContact(string nombre, string email, string telefono, string comentario)
{
MailMessage mail = new MailMessage();
mail.To.Add("addressee@example.com");
mail.From = new MailAddress(email, nombre);
mail.Sender = new MailAddress(email, nombre);
mail.Subject = "Solicito informacion";
mail.Body = telefono + comentario;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("my.email@gmail.com","my.pass");
smtp.EnableSsl = true;
smtp.Send(mail);
}