2013-03-26 77 views
0

我有一个MVC Razor Web应用程序,它具有反馈表单和数据库后端。如何使用MVC Razor通过反馈表发送电子邮件

我们填写反馈表并点击提交,信息进入数据库。

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

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

回答

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 = "[email protected]"; 
    WebMail.Password = "yourpassword"; 
    WebMail.From = "[email protected]"; 
    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); 
    } 
+0

感谢这个,如果有什么发送反馈没有Gmail帐户的人? – Reemi 2013-03-26 12:23:05

+0

提供您想要发送邮件的域的smtp详细信息。我以gmail为例 – Sham 2013-03-26 12:31:25