我尝试在 mvc 4 中创建一个联系表单,它可以工作,但发件人(电子邮件和姓名)始终与我在 smtp 凭据中使用的相同,我想显示电子邮件和发送它的用户的姓名从我的联系表格中,有什么帮助吗?这是我的代码:
模型:
[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; }
控制器:
[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);
}