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);
}