ASP.NET电子邮件表单提交表单后返回消息

时间:2012-01-31 20:58:08

标签: asp.net-mvc model-view-controller asp.net-mvc-2

对不起,如果已经解决了这个问题。我无法让它发挥作用。我有一个电子邮件联系表格。我的表格工作正常。它正在发送电子邮件。但是,在提交表单后,我要么弹出一条消息,要么被重定向到另一个页面,说明他们的电子邮件已成功发送,我们会尽可能地回复。我已经尝试过RedirectActionTo和其他一些东西,但我无法让它工作。这是我的代码。

       public ActionResult ContactForm(ContactModel emailModel)
    {            
            MailMessage oMail = new MailMessage();

            oMail.From = new MailAddress("no-reply@domain.com", "Web Contact Form");
            oMail.To.Add("email@domain.com");
            oMail.Subject = emailModel.Subject;
            string body = "Name: " + emailModel.Name + "\n"
                        + "Email: " + emailModel.Email + "\n"
                        + "Website: " + emailModel.Website + "\n"
                        + "Phone: " + emailModel.Phone + "\n\n"
                        + emailModel.Message;

            oMail.Body = body;

            if (SendMessage(oMail))
            {
                return RedirectToAction("Message");
            }
            else
            {
                return RedirectToAction("Error");
            }
            return View();
    }
    private bool SendMessage(MailMessage oMail)
    {
        SmtpClient client = new SmtpClient("relay-hosting.secureserver.net");
        client.Credentials = new NetworkCredential("email@domain.com", "********", "domaion.com");
        try
             {
                 client.Send(oMail);
                 return true;
             }
             catch (Exception ex)
             {
                 this.exception = ex;
                 return false;
             } }

}

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您的IIS设置不正确,这也已过时,您应该使用System.Net.Mail.SmtpClient,请参阅SocttGu's blog帖子

答案 1 :(得分:0)

原因是因为行动完成而没有等待结果。

这是我的实现的简化版本

在您的控制器中,您可以设置您的回复,例如:

public class ContactModel
        {
            public string Name { get; set; }
            public string Email { get; set; }
            public string Website { get; set; }
            public string Phone { get; set; }
            public string Message { get; set; }
            public string Subject { get; set; }
        }

        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            ContactModel model = new ContactModel();
            model.Email = "test@email.com";
            model.Message = "message";
            model.Name = "name";
            model.Subject = "subject";
            model.Website = "http://test.com";
            ContactForm(model);

            return View();
        }

        /// <summary>
        /// Contacts the form.
        /// </summary>
        /// <returns>Returns view to display the form and fill</returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ContactForm()
        {
            return View(new ContactModel());
        }

        /// <summary>
        /// Contacts the form.
        /// If model is valid we will go ahead and process emailing 
        /// </summary>
        /// <param name="emailModel">The email model.</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ContactForm(ContactModel emailModel)
        {
            if (ModelState.IsValid)
            {

                MailMessage oMail = new MailMessage();

                oMail.From = new MailAddress("no-reply@domain.com", "Web Contact Form");
                oMail.To.Add("email@domain.com");
                oMail.Subject = emailModel.Subject;
                string body = "Name: " + emailModel.Name + "\n"
                              + "Email: " + emailModel.Email + "\n"
                              + "Website: " + emailModel.Website + "\n"
                              + "Phone: " + emailModel.Phone + "\n\n"
                              + emailModel.Message;

                oMail.Body = body;

                if (SendMessage(oMail))
                {
                    return RedirectToAction("Message");
                }
                else
                {
                    return RedirectToAction("Error");
                }
            }
            else
            {
                return View(emailModel);
            }
        }

        /// <summary>
        /// Sends the message.
        /// </summary>
        /// <param name="oMail">The o mail.</param>
        /// <returns>Boolean success.</returns>
        private bool SendMessage(MailMessage oMail)
        {
            SmtpClient client = new SmtpClient("relay-hosting.secureserver.net");
            client.Credentials = new NetworkCredential("email@domain.com", "********", "domaion.com");
            try
            {
                client.Send(oMail);
                return true;
            }
            catch (Exception ex)
            {
                this.exception = ex;
                return false;
            }
        }

视图正在重定向,因为我猜你没有在方法上拆分请求的类型。因此它直接重定向。

这是由

完成的
  

[AcceptVerbs(Http.Get)]或[AcceptVerbs(Http.Post)]

希望这会有所帮助

  

传递讯息:

TemData["message"] = "pass message between controllers like this"
     

public AcionResult Message(string message){//这里选择   ViewData [“message”] = TempData [“message];

     

return View(); }

     

    return RedirectToAction("Message", new{message = "pass message between controllers"});
 `enter code here`    where 
public AcionResult Message(string message){
//or if you are using parameter
ViewData["message"] = message;


return View();
}