2017-10-27 6 views
0

문의 양식을 가지고 있으며 이메일에 고객 이름 ("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; } 
} 
+0

일 . 파일에서이 파일을 읽고 마술처럼 HTML을 만들 것을 기대할 수는 없습니다. – mason

답변

0

보기에 표시된 "모델"이 표시되지 않습니다. 보기 코드 상단에 모델을 추가하는 것만 큼 간단합니까? @model Namespace.Models.ContactModel 귀하의 모든 전달이 이름 인 경우 모델 @model String에 "문자열"을 사용하고 아래 @Model.Name 대신 @Model을 사용할 수 있습니다.

@model Namespace.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> 
+0

네, 그게 무슨 이유인지는 모르겠지만 이메일이 모든 코드를 읽습니다. – theone

+1

그가 이메일 본문을 어떻게 보았습니까? 디스크에서 cshtml을 읽기 만하면됩니다. 그게 효과가 없을거야. – mason

0

가 나는

내가보기에 {이름}을 넣어 내 컨트롤러에이 코드를 추가했고 완벽하게

당신은 실제로 cshtml에 대한 면도기 엔진을 실행해야
    CustomerMsg.Body = string.Empty; 
      using (StreamReader reader = new StreamReader(Server.MapPath("~/Templates/Email.html"))) 
      { 
       CustomerMsg.Body = reader.ReadToEnd(); 
      } 


      CustomerMsg.Body = CustomerMsg.Body.Replace("{name}", model.Name.ToUpper());