我有一个要求客户填写的表格。提交表单后,我想将表单的“索引”视图中的基本信息(名字,姓氏,电话号码等)发送到电子邮件中。我目前正在将GoDaddy用于我的托管站点。这有关系吗,或者我可以直接从MVC应用程序发送电子邮件吗?我的模型,视图,控制器有以下内容。我以前从未做过,而且真的不确定该怎么做。
模型:
public class Application
{
public int Id { get; set; }
[DisplayName("Marital Status")]
public bool? MaritalStatus { get; set; }
[Required]
[DisplayName("First Name")]
public string FirstName { get; set; }
[DisplayName("Middle Initial")]
public string MiddleInitial { get; set; }
[Required]
[DisplayName("Last Name")]
public string LastName { get; set; }
}
控制器:
public ActionResult Index()
{
return View();
}
// POST: Applications/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "Id,FirstName,MiddleInitial,LastName")] Application application)
{
ViewBag.SubmitDate = DateTime.Now;
if (ModelState.IsValid)
{
application.GetDate = DateTime.Now;
db.Applications.Add(application);
db.SaveChanges();
return RedirectToAction("Thanks");
}
return View(application);
}
看法
<table class="table table-striped">
<tr>
<th>
@Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
<th>
@Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
<th>
@Html.ActionLink("Date Submitted", "Index", new { sortOrder = ViewBag.NameSortParm})
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.GetDate)
</td>
</tr>
}
您将需要SMTP服务器来发送电子邮件。不知道GoDaddy如何运作,但我敢肯定他们会提供一些东西。
要从MVC应用发送电子邮件,您可以在代码中或在中指定SMTP详细信息web.config
。我建议在配置文件中,因为它意味着更容易更改。一切都在web.config中:
SmtpClient client=new SmtpClient();
否则,请在代码中执行此操作:
SmtpClient client=new SmtpClient("some.server.com");
//If you need to authenticate
client.Credentials=new NetworkCredential("username", "password");
现在创建消息:
MailMessage mailMessage = new MailMessage();
mailMessage.From = "[email protected]";
mailMessage.To.Add("[email protected]");
mailMessage.Subject = "Hello There";
mailMessage.Body = "Hello my friend!";
最后发送:
client.Send(mailMessage);
web.config
设置示例:
<system.net>
<mailSettings>
<smtp>
<network host="your.smtp.server.com" port="25" />
</smtp>
</mailSettings>
</system.net>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句