C# 如何在MVC中在电子邮件中包含表单内容
我在提交表格时发送电子邮件。如何在电子邮件正文部分包含表单内容 我正在使用db.Employee.Add(Employee)将表单内容保存到mydb中强> 我用字符串文本指定电子邮件文本 下面你可以看到代码和我的表格C# 如何在MVC中在电子邮件中包含表单内容,c#,asp.net-mvc,sendmail,C#,Asp.net Mvc,Sendmail,我在提交表格时发送电子邮件。如何在电子邮件正文部分包含表单内容 我正在使用db.Employee.Add(Employee)将表单内容保存到mydb中 我用字符串文本指定电子邮件文本 下面你可以看到代码和我的表格 namespace MvcApplication8.Controllers { public class PTFController : Controller { private UsersContext db = new UsersContext(); [HttpPost
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("receiver@somemail.com", Text);
return RedirectToAction("Index");
}
return View(employee);
}
public static bool SendEmail(string SentTo, string Text)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("noreply@myemail.com");
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;
}
}
}
namespace mvcapapplication8.Controllers
{
公共类PTFController:控制器
{
private UsersContext db=new UsersContext();
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果创建(员工)
{
if(ModelState.IsValid)
{
db.Employee.Add(员工);
db.SaveChanges();
string Text=“”+
" " +
????? +
"";
发送电子邮件(“receiver@somemail.com“,案文);
返回操作(“索引”);
}
返回视图(员工);
}
公共静态bool sendmail(字符串SentTo,字符串文本)
{
MailMessage msg=新的MailMessage();
msg.From=新邮件地址(“noreply@myemail.com");
msg.To.Add(SentTo);
msg.Subject=“新邮件”;
msg.Body=文本;
msg.IsBodyHtml=true;
SmtpClient=新的SmtpClient(“mysmtp.address.com”,25);
client.UseDefaultCredentials=false;
client.enablesl=false;
client.Credentials=新的网络凭据(“mywindowsusername”、“mypassword”);
client.DeliveryMethod=SmtpDeliveryMethod.Network;
//client.enablesl=true;
尝试
{
client.Send(msg);
}
捕获(例外)
{
返回false;
}
返回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>
创建
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
雇员
@LabelFor(model=>model.EmployeeName)
@EditorFor(model=>model.EmployeeName)
@Html.ValidationMessageFor(model=>model.EmployeeName)
@LabelFor(model=>model.DepartmentID)
@EditorFor(model=>model.DepartmentID)
@Html.ValidationMessageFor(model=>model.DepartmentID)
有些库可以使用Razor引擎(甚至与MVC分离)作为电子邮件模板系统。您可以使用Razor模板作为电子邮件正文。这篇文章已经讨论过: