【问题标题】:MVC3 Contact Form not Sending Email After POSTing发布后 MVC3 联系表单未发送电子邮件
【发布时间】:2012-09-08 21:06:06
【问题描述】:

我构建了一个简单的联系表单,该表单在我的测试“mvc1application”中非常适用。当我将确切的代码转移到我的一个网站时,我会遇到问题并且真的很难过。

场景是当用户访问网站时,它会检查他们是否已经通过身份验证(通过 SSO 系统)。如果是这样,则设置了 cookie 并且他们已登录。如果您尝试在未登录的情况下访问该站点,您当前会被重定向到“AccessDenied.cshtml”视图。这完全没有问题。

我已经用一个表单(简单的联系表单)替换了该视图,用户可以填写该表单以向网站管理员发送电子邮件。我让表单正确显示,在 POST 上它得到 200,通过 Chrome 开发工具,我看到表单内容已发布。没有发生的是我认为我的 EmailViewModel.cs 没有被调用并处理表单数据。所以从来没有发送过电子邮件。

我真的很难过,这里是一些示例代码:

(我的 AccountController 的一些部分被剪掉了......)

[AllowAnonymous]
public ActionResult LogOn(string strEmail, string token)
{
    DepartmentUser departmentuser = db.DepartmentUsers.SingleOrDefault(r => r.UserEmail == strEmail && r.Token == token);
    if (departmentuser != null)
    {
        //verify that the token date  + 1 day >= current datetime
        if (departmentuser.TokenDate.Value.AddDays(1) >= DateTime.Now)
        {
            //if yes, then update to set token = null and datetime = null
            departmentuser.Token = null;
            departmentuser.TokenDate = null;
            db.SaveChanges();

            string userData = departmentuser.DepartmentUserID.ToString() + ":" + departmentuser.DepartmentID.ToString() + ":" + departmentuser.UserName;

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, departmentuser.UserEmail,
                DateTime.Now, DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
                false, userData);
            string hashedTicket = FormsAuthentication.Encrypt(ticket);

            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashedTicket);
            Response.AppendCookie(cookie);
            return Redirect("~/Home");
        }
        else
        {
            return View("RequestForm");

        }
    }
    else
    {
        return View("RequestForm");
    }
}

public ActionResult RequestForm(EmailViewModel emailVM)
{
    if (!ModelState.IsValid)
    {
        return View(emailVM);
    }

    var email = new EmailViewModel
    {

        Name = emailVM.Name,
        EmailAddress = emailVM.EmailAddress,
        InternalTeamName = emailVM.InternalTeamName,
        InternalTeamLeadName = emailVM.InternalTeamLeadName,
        InternalDistEmail = emailVM.InternalDistEmail,
        AverageNumCommsWeekly = emailVM.AverageNumCommsWeekly,
        AverageNumPeopleSentWeekly = emailVM.AverageNumPeopleSentWeekly
    };

    string body = "Name: " + email.Name + "\n"
                + "Email: " + email.EmailAddress + "\n"
                + "Internal Team: " + email.InternalTeamName + "\n"
                + "Internal Team Lead: " + email.InternalTeamLeadName + "\n"
                + "Internal Team Lead Email: " + email.InternalTeamLeadEmail + "\n"
                + "Internal Distribution Email: " + email.InternalDistEmail + "\n"
                + "Average # of Communications Per Week: " + email.AverageNumCommsWeekly + "\n"
                + "Average # of People Emailed Per Week: " + email.AverageNumPeopleSentWeekly;

    MailMessage mail = new MailMessage();

    mail.From = new MailAddress(email.EmailAddress);
    // for production put email in web.config for easy change
    mail.To.Add("user@email.me");
    mail.Subject = "Access Request";
    mail.Body = body;
    mail.IsBodyHtml = true;

    // smtp is local directory in web.config for testing ATM...
    SmtpClient smtp = new SmtpClient();

    smtp.Send(mail);

    mail.Dispose();
    return View("RequestForm");

}

还有我的 EmailViewModel.cs...

namespace MVC.Models
{
    public class EmailViewModel
    {
        [Required]
        public string Name { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        public string EmailAddress { get; set; }

        [Required]
        public string InternalTeamName { get; set; }

        [Required]
        public string InternalTeamLeadName { get; set; }

        [Required]
        public string InternalTeamLeadEmail { get; set; }

        [Required]
        public string InternalDistEmail { get; set; }

        [Required]
        public string AverageNumCommsWeekly { get; set; }

        [Required]
        public string AverageNumPeopleSentWeekly { get; set; }


    }
}

最后是我的 Requestform.cshtml 视图...

@model CorpNewsMgr.Models.EmailViewModel
@{
    ViewBag.Title = "Request Access";
    Layout = "~/Views/Shared/_WideLayoutDenied.cshtml";
}
<p>
    Currently you do not have the required credentials to access the site.
    <br />
    To request more information, or to request permission to access this system, please
    contact:
</p>
<br />
<br />
<div>
    <h3>
        Please fill this form out to request access to the tool.</h3>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Request Access</legend>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.Name)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.Name)
                @Html.ValidationMessageFor(Model => Model.Name)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.EmailAddress)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.EmailAddress)
                @Html.ValidationMessageFor(Model => Model.EmailAddress)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamName)
                @Html.ValidationMessageFor(Model => Model.InternalTeamName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamLeadName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamLeadName)
                @Html.ValidationMessageFor(Model => Model.InternalTeamLeadName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamLeadEmail)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamLeadEmail)
                @Html.ValidationMessageFor(Model => Model.InternalTeamLeadEmail)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalDistEmail)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalDistEmail)
                @Html.ValidationMessageFor(Model => Model.InternalDistEmail)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.AverageNumCommsWeekly)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.AverageNumCommsWeekly)
                @Html.ValidationMessageFor(Model => Model.AverageNumCommsWeekly)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.AverageNumPeopleSentWeekly)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.AverageNumPeopleSentWeekly)
                @Html.ValidationMessageFor(Model => Model.AverageNumPeopleSentWeekly)
            </div>
            <p>
                <input type="submit" value="Send" />
            </p>
        </fieldset>

    }
</div>
<br />
<p>
    Thank you,<br />

</p>

所以当页面加载时表单加载正常(耶)。我可以填写并验证它(耶)。在提交时我得到一个 200 并且 POST 看起来很好(我可以看到 POST 中的字段)但没有任何反应。页面重新加载为空表单,我的取件文件夹或任何内容中都看不到任何电子邮件。

再次,在我的本地开发测试站点上,我可以让它正常工作并通过路由。在这里我不能。我认为问题在于 AccountController 的顶部以及它如何拉动视图,但我很难(并且很累)试图弄清楚这一点。

哦,这是我第一次尝试从标准 WebForms 构建 MVC 表单...

有什么想法吗?

谢谢!

【问题讨论】:

  • 您需要有一个操作结果来处理表单的发布。
  • 终于搞定了。我最终不得不将[AllowAnonymous] 添加到我的控制器操作中,因为它一直在尝试重定向和循环系统。不过,感谢您的想法。

标签: asp.net-mvc-3 razor


【解决方案1】:

我本来想评论的,但是太长了。

您需要发布Requestform.cshtml 发布到的操作,这里也不需要您的帐户控制器代码。我的猜测是您的托管环境是共享托管,并且有一些东西阻止您的电子邮件代码运行。

您可能想要做的第一件事是,如果您的电子邮件代码在 try catch 内,请暂时评论我们的 try catch,这样它就会抛出错误。这至少应该可以帮助您了解正在发生的事情。

接下来要做的是尝试为 mvc3 添加Elmah,您可以通过 Visual Studios 中的 Nuget 添加它。确保将 Elmah 用于 MVC3,因为它会为您完成大部分设置。这将让您看到是否有任何错误被抛出。

否则,如果没有看到您的实际电子邮件代码,我将无法提供更多帮助。

【讨论】:

  • 主机现在在我的内部开发箱上。专用站点/托管。我把控制器代码放在那里,因为我看到了我正在关注的一个例子(就像我说我是 MVC 的新手)。我将尝试 try/catch 语句,看看是否能找出问题所在。
  • 所以当你说你的代码在你的测试应用程序中运行时,它运行在什么之上,IIS express 还是 IIS?可能存在 IIS 设置或库版本问题。还要确保两个应用程序使用相同的电子邮件库dll 版本(如果有的话),因为这可能会导致这样的问题。
猜你喜欢
  • 2019-10-03
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 2018-04-15
  • 2020-05-20
  • 2020-03-23
相关资源
最近更新 更多