我建立了一個簡單的聯繫表格,在我的測試'mvc1application'中在本地很好地工作。當我將確切的代碼轉移到我的網站之一,這個表單將是我遇到的問題,我真的難住。MVC3聯繫表格郵寄後不發送電子郵件
情景是當用戶點擊它檢查的網站時,他們是否已通過SSO系統進行身份驗證。如果是,則設置cookie並登錄。如果您嘗試在未登錄的情況下訪問網站,則當前將重定向到「AccessDenied.cshtml」視圖。這完全沒有問題。
我已經用一個表單(簡單的聯繫表單)替換了這個View,用戶可以通過這個表單來發送電子郵件給Web主機。我得到的表單正確顯示,並在POST上獲得200和通過Chrome開發工具,我看到表單內容POSTED。沒有發生的事情是我不認爲我的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("[email protected]");
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>
所以當頁面加載時,表單加載正常(yay)。我可以填寫並驗證它是否正確(耶)。在提交我得到一個200和POST看起來很好(我可以看到POST中的字段),但沒有任何反應。該頁面將作爲空白表單重新加載,並且我的皮卡文件夾中沒有看到任何電子郵件或任何內容。
再次,在我的本地開發測試網站上,我可以讓它工作並通過OK路由。我不能。我認爲問題在於AccountController的頂部,以及它如何拉動視圖,但是我很難弄清楚(而且很累)。
哦,這是我的第一個大進軍,從標準的WebForms MVC形式建設...
有什麼想法?
謝謝!
您需要有一個操作結果來處理表單的發佈。 – Chris
終於搞定了。我最終不得不將'[AllowAnonymous]'添加到我的Controller操作中,因爲它一直試圖重定向和循環系統。感謝這裏的想法。 – Valien