2014-09-13 29 views
0

我在表單提交時發送電子郵件。我如何在電子郵件正文部分包含表單內容?如何在MVC中的電子郵件中包含表單內容

我將表單內容保存到mydb中db.Employee.Add(employee); 我指定我的電子郵件文本與字符串文本

下面你可以看到代碼和我的表單。

namespace MvcApplication8.Controllers 
{ 
public class PTFController : Controller 
{ 
    private UsersContext db = new UsersContext(); 

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(Employee employee) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Employee.Add(employee); 
      db.SaveChanges(); 

      string Text = "<html> <head> </head>" + 
    " <body style= \" font-size:12px; font-family: Arial\">" + 
    ????? + 
    "</body></html>"; 

      SendEmail("[email protected]", Text); 

      return RedirectToAction("Index"); 
     } 

     return View(employee); 
    } 

    public static bool SendEmail(string SentTo, string Text) 
    { 
     MailMessage msg = new MailMessage(); 

     msg.From = new MailAddress("[email protected]"); 
     msg.To.Add(SentTo); 
     msg.Subject = "New email"; 
     msg.Body = Text; 
     msg.IsBodyHtml = true; 

     SmtpClient client = new SmtpClient("mysmtp.address.com", 25); 



     client.UseDefaultCredentials = false; 
     client.EnableSsl = false; 
     client.Credentials = new NetworkCredential("mywindowsusername", "mypassword"); 
     client.DeliveryMethod = SmtpDeliveryMethod.Network; 
     //client.EnableSsl = true; 

     try 
     { 
      client.Send(msg); 
     } 
     catch (Exception) 
     { 
      return false; 
     } 
     return true; 
    } 
    } 
    } 

這是以下形式:

<h2>Create</h2> 

    @using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
    <legend>Employee</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.EmployeeName) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.EmployeeName) 
     @Html.ValidationMessageFor(model => model.EmployeeName) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.DepartmentID) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.DepartmentID) 
     @Html.ValidationMessageFor(model => model.DepartmentID) 
    </div> 

回答

1

有一些庫使用剃刀引擎(甚至從MVC分離)作爲電子郵件的模板系統。您可以使用Razor模板作爲電子郵件正文。這已經在這篇文章中討論過:Razor views as email templates

相關問題