如何从MVC 5应用程序发送电子邮件 [英] How to send email from MVC 5 application

查看:126
本文介绍了如何从MVC 5应用程序发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

模型:

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

控制器:

 公众的ActionResult指数()
{
        返回查看();
}// POST:应用程序/创建
//从overposting攻击保护,请启用要绑定到特定的属性,
//更多详情请参阅http://go.microsoft.com/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.com);
//如果你需要验证
client.Credentials =新的NetworkCredential(用户名,密码);

现在您创建消息:

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

最后发送:

  client.Send(MAILMESSAGE);

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

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

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

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.com");
//If you need to authenticate
client.Credentials=new NetworkCredential("username", "password");

Now you create your message:

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

Finally send it:

client.Send(mailMessage);

An example for the web.config set up:

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

这篇关于如何从MVC 5应用程序发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆