如何从MVC 5应用程序发送电子邮件

编程入门 行业动态 更新时间:2024-10-25 05:23:10
本文介绍了如何从MVC 5应用程序发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个客户需要填写一份表格。一旦提交表单,我想从发送窗体的索引视图的基本信息(姓,名,电话号码等),以电子邮件。我目前使用GoDaddy的我的托管网站。请问这件事情,还是可以我直接从我的MVC应用程序发送电子邮件?我有我的模型,视图,控制器以下。我以前从没做过,真的不知道如何去做。

模型:的

公共类应用{    公众诠释标识{搞定;组; }    [显示名称(婚姻状况)]    公共BOOL? MaritalStatus {搞定;组; }    [需要]    [显示名称(名)]    公共字符串名字{获得;组; }    [显示名称(中间名)]    公共字符串MiddleInitial {搞定;组; }     [需要]    [显示名称(姓氏)]    公共字符串名字{获得;组; }}

控制器:的

公众的ActionResult指数(){        返回查看();}// POST:应用程序/创建//从overposting攻击保护,请启用要绑定到特定的属性,//更多详情请参阅go.microsoft/fwlink/?LinkId=317598。[HttpPost][ValidateAntiForgeryToken]公众的ActionResult指数([绑定(包括=ID,名字,MiddleInitial,姓氏)]应用程序){    ViewBag.SubmitDate = DateTime.Now;    如果(ModelState.IsValid)    {        application.GetDate = DateTime.Now;        db.Applications.Add(应用);        db.SaveChanges();        返回RedirectToAction(感谢);    }    返回查看(应用);}

查看的

<表类=表的表条纹>    &所述; TR>        <第i个           @ Html.ActionLink(名,索引,新的{中将sortOrder = ViewBag.NameSortParm})        < /第i        <第i个            @ Html.ActionLink(姓,索引,新的{中将sortOrder = ViewBag.NameSortParm})        < /第i        <第i个            @ Html.ActionLink(日期已提交,索引,新的{中将sortOrder = ViewBag.NameSortParm})        < /第i    < / TR>@foreach(以型号VAR项){    &所述; 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细节code或的web.config 。我在配置文件中建议,因为它意味着它更容易改变。随着在web.config中的一切:

SmtpClient客户端=新SmtpClient();

另外,做在code:

SmtpClient客户端=新SmtpClient(some.server);//如果你需要验证client.Credentials =新的NetworkCredential(用户名,密码);

现在您创建消息:

MAILMESSAGE MAILMESSAGE新= MAILMESSAGE();mailMessage.From =someone@somewhere;mailMessage.To.Add(someone.else@somewhere-else);mailMessage.Subject =你好;mailMessage.Body =你好,我的朋友!

最后发送:

client.Send(MAILMESSAGE);

为的web.config 设置了一个例子:

< system>    < mailSettings>        <&SMTP GT;            <网络主机=your.smtp.server端口=25/>        < / SMTP>     < / mailSettings>< /system>

I have a form that a customer is required to fill out. Once the form is submitted, I'd like to send the basic information from the form's Index view (First Name, Last Name, Phone Number, etc..) to an email. I'm currently using GoDaddy for my hosting site. Does this matter, or can I send the email directly from my MVC application? I have the following for my Model, View, Controller. I've never done this before and am really not sure how to go about it.

Model:

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; } }

Controller:

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 go.microsoft/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); }

View

<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> }

解决方案

You will need an SMTP server to send email from. No idea how GoDaddy works but I'm sure they will provide something.

To send emails from an MVC app you either specify you SMTP details in code or in the web.config. I recommend in the config file as it means it's much easier to change. With everything in the web.config:

SmtpClient client=new SmtpClient();

Otherwise, do it in code:

SmtpClient client=new SmtpClient("some.server"); //If you need to authenticate client.Credentials=new NetworkCredential("username", "password");

Now you create your message:

MailMessage mailMessage = new MailMessage(); mailMessage.From = "someone@somewhere"; mailMessage.To.Add("someone.else@somewhere-else"); mailMessage.Subject = "Hello There"; mailMessage.Body = "Hello my friend!";

Finally send it:

client.Send(mailMessage);

An example for the web.config set up:

<system> <mailSettings> <smtp> <network host="your.smtp.server" port="25" /> </smtp> </mailSettings> </system>

更多推荐

如何从MVC 5应用程序发送电子邮件

本文发布于:2023-10-13 00:21:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1486280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   发送电子邮件   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!