문의 양식을 가지고 있으며 이메일에 고객 이름 ("Dear John Doe")을 전달하고 싶습니다. 표시 할 이름을 얻으려면 어떻게해야합니까? asp mvc의 이메일에 고객 이름을 추가하는 방법
은 내가 @의 Model.Name를 호출 시도 뷰에 모델 클래스를 호출되고 이메일 코드 대신에 값을 집어내보기
@model MyProject.Models.ContactModel;
<!DOCTYPE html>
<html>
<head>
<title>Thank You</title>
</head>
<body>
<h1>Confirmation</h1>
<p>
Dear @Model.Name, <br /><br />
Thank you for contacting us. This is system generated response
</p>
</body>
</html>
컨트롤러
[HttpPost]
public async Task<ActionResult> Index(ContactModel model)
{
if (ModelState.IsValid)
{
MailMessage CustomerMsg = new MailMessage();
CustomerMsg.IsBodyHtml = true;
CustomerMsg.From = new MailAddress("example.com [email protected]");
CustomerMsg.To.Add(model.EmailContact);
CustomerMsg.Subject = "Thank you for your submission";
CustomerMsg.Body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/Views/Contact/EmailTemplate.cshtml")))
{
CustomerMsg.Body = reader.ReadToEnd();
}
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "",
Password = ""
};
smtp.Credentials = credential;
smtp.Host = "mail.example.com";
smtp.Port = 587;
smtp.EnableSsl = false;
try
{
await smtp.SendMailAsync(message);
await smtp.SendMailAsync(CustomerMsg);
}
catch (Exception e)
{
return RedirectToAction("Error", "Home");
}
return RedirectToAction("Sent", "Home");
}
}
return View(model);
모델
public class ContactModel
{
public string error { get; set; }
[Required(ErrorMessage = " Name field required")]
public string Name { get; set; }
[Required(ErrorMessage = " Email field required")]
[EmailAddress(ErrorMessage = " Valid email required")]
public string EmailContact { get; set; }
[Required(ErrorMessage = " Feedback required")]
public string Message { get; set; }
}
일 . 파일에서이 파일을 읽고 마술처럼 HTML을 만들 것을 기대할 수는 없습니다. – mason