通过ViewBag显示消息“Email sending failed / successful”asp.net mvc 4

时间:2013-06-03 12:11:49

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

我在我的mvc网站上有反馈表,我将此表发送给电子邮件 我想显示错误消息,以防电子邮件发送失败,成功消息以防电子邮件发送成功。我尝试通过ViewBag制作它。
我在控制器中添加了

    [HttpGet]
    public ActionResult Feedback(string Message)
    {
        if (Message != null)
        {
            if (Message == "No")
            {
                ViewBag.Message = "Error";
            }

            if (Message == "Yes")
            {
                ViewBag.Message = "Success";
            }
        }

        else
        {
            ViewBag.Message = null;
        }

        return View();
    }

    [HttpPost]
    public ActionResult Feedback(FeedbackForm Model)
    {
        string Message;

        //email
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.BodyEncoding = Encoding.UTF8;
        msg.Priority = MailPriority.High;

        msg.From = new MailAddress(Model.Email, Model.Name);
        msg.To.Add(/*"evaluation@corepartners.ru"*/"tayna-anita@mail.ru");

        msg.Subject = @Resources.Global.Feedback_Email_Title + " " + Model.Company;
        string message = @Resources.Global.Feedback_Email_From + ": " + Model.Name + "\n"
                        + @Resources.Global.Feedback_Email + ": " + Model.Email + "\n"
                        + @Resources.Global.Feedback_Phone + ": " + Model.Phone + "\n"
                        + @Resources.Global.Feedback_Company + ": " +  Model.Company + "\n\n"
                        + Model.AdditionalInformation;
        msg.Body = message;
        msg.IsBodyHtml = false;

        //Attachment
        if (Model.ProjectInformation != null && !(String.IsNullOrEmpty(Model.ProjectInformation.FileName)))
        {
            HttpPostedFileBase attFile = Model.ProjectInformation;
            if (attFile.ContentLength > 0)
            {
                var attach = new Attachment(attFile.InputStream, attFile.FileName);
                msg.Attachments.Add(attach);
            }
        }

        SmtpClient client = new SmtpClient("denver.corepartners.local", 55);
        client.UseDefaultCredentials = false;
        client.EnableSsl = false;

        try
        {
            client.Send(msg);
            return RedirectToAction("Feedback", "Home", Message = "Yes");
        }

        catch (Exception ex)
        {
            return RedirectToAction("Feedback", "Home", Message = "No");
        }
    }

我在视图中添加了

    @if (ViewBag.Message != null)
    {
        <p style="color: red;">@ViewBag.Message</p>
    }

但在任何情况下我都没有收到任何消息 怎么了?

1 个答案:

答案 0 :(得分:3)

试试这个:

return RedirectToAction("Feedback", "Home", new { Message = "Yes" });