0
我尝试在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("[email protected]");
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("[email protected]","my.pass");
smtp.EnableSsl = true;
smtp.Send(mail);
}