如何使用MVC Razor通过反馈表发送电子邮件

时间:2013-03-26 11:41:12

标签: asp.net-mvc razor

我有一个MVC Razor Web应用程序,它有一个反馈表和一个数据库后端。

Wehen我填写反馈表单并单击“提交”,信息将进入数据库。

我的问题是如何将此信息通过电子邮件发送到电子邮件地址,代码如下:

[HttpPost]
        public ActionResult Feedback(FeedbackModel model)
        {
            if (ModelState.IsValid)
            {
                if (this.db.InsertFeedback(model))
                {
                    return RedirectToAction("FeedbackSuccessful");
                }
                else
                {
                    ModelState.AddModelError("", "Error! We were unable to process your feedback at this time. Please try again later.");
                }
            }
            return View(model);
        }

1 个答案:

答案 0 :(得分:0)

Try this and add required references.  (using System.Web.Helpers;)  

[HttpPost]

    public ActionResult Feedback(FeedbackModel model)
    {
    if (ModelState.IsValid)
    {
    if (this.db.InsertFeedback(model))
    {
     WebMail.SmtpServer = "smtp.gmail.com";
    WebMail.SmtpPort = 587;
    WebMail.EnableSsl = true;
    WebMail.UserName = "youremailID@gmail.com";
    WebMail.Password = "yourpassword";
    WebMail.From = "from@gmail.com";
    WebMail.Send("Email Address", "Mail Subject", "Mail Body");
return RedirectToAction("FeedbackSuccessful");
    }
    else
    {
    ModelState.AddModelError("", "Error! We were unable to process your feedback at this time. Please try again later.");
    }
    }
    return View(model);
    }