2012-09-07 176 views
0

我建立了一個簡單的聯繫表格,在我的測試'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形式建設...

有什麼想法?

謝謝!

+0

您需要有一個操作結果來處理表單的發佈。 – Chris

+0

終於搞定了。我最終不得不將'[AllowAnonymous]'添加到我的Controller操作中,因爲它一直試圖重定向和循環系統。感謝這裏的想法。 – Valien

回答

1

我打算髮表評論,但它跑了太久。

您需要發佈Requestform.cshtml發佈的操作,同時您的帳戶控制器代碼在這裏也不是真的需要。我的猜測是您的主機環境是共享主機,並且有些東西阻止您的電子郵件代碼正常工作。

您可能想要做的第一件事是,如果您的電子郵件代碼在try catch之內,請暫時評論我們的try catch,以便發現錯誤。這至少應該能幫助你瞭解發生了什麼。

接下來要做的是嘗試爲mvc3添加Elmah,您可以在Visual Studios中通過Nuget添加此項。請確保使用Elmah for MVC3,因爲它將爲您安排一切。這將允許你看到它有任何錯誤被拋出。

否則沒有看到您的實際電子郵件代碼我不能有更多的幫助。

+0

主機現在在我的內部開發框中。專用網站/託管。我把控制器的代碼放在那裏,就像我看到的一個例子(就像我說我是MVC的新手)。我會嘗試try/catch語句,看看我能否找出它拋出問題的地方。 – Valien

+0

因此,當你說你的測試應用程序中有代碼工作時,它運行什麼,IIS express或IIS?也許有一個IIS設置或庫版本問題。此外,請確保這兩個應用程序都使用相同的電子郵件庫'dll'版本(如果有的話)可能會導致此類問題的原因。 –