抱歉,在托管网站

时间:2015-04-24 10:24:25

标签: asp.net asp.net-mvc-4 smtp sendmail

最近我发布了asp.net MVC4网站。当我提交所有细节后,我转到联系表单{J} Sorry, an error occurred while processing your request。如何解决这个问题?这背后的原因是什么。联系表格在localhost中顺利运行。每当我们提交表格时,详细信息都会发送到邮件

这是我的代码

[HttpPost]
    public ActionResult Contact(form model)
    {
        if (ModelState.IsValid)
        {
            var result = "";
            TempData["notice"] = "Your form is submitted";
            //write to txt file
            var name = Request["Name"];
            var email = Request["Email"];
            var phone = Request["Phone"];
            var message = Request["Message"];
            var subject = "contact us";
            var userdata = "Name :" + name + "<br/>" + "Email id :" + email + "<br/>" + "Phone Number :" + phone + "<br/>" + "<b/>Message :" + "<br/>" + message;
            //var datafile = Server.MapPath("~/App_Data/data.txt");
            //System.IO.File.AppendAllText(@datafile,userdata);
            //result = "information saved";
            var body = userdata;


            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(email.ToString());
            msg.To.Add("mail id here");
            msg.Subject = "Contact Us";
            msg.IsBodyHtml = true;
            msg.Body = userdata;

            //smtp settings
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("mail id", "password");
            smtp.Timeout = 20000;
            smtp.Send(msg);



            return RedirectToAction("Contact", "Home");
        }

0 个答案:

没有答案