2013-11-25 19 views
0

我想讓用戶填寫一個建議表單。該表格旨在發送到我的管理員的電子郵件地址。沒有收到任何電子郵件。這是我的看法。我是編程初學者。由於在IDE中沒有顯示錯誤,因此不確定我所缺少的。爲什麼我沒有收到來自我的MVC 4 Razor網站的電子郵件?

@model DonsSolution.Models.MailModel 

<h2>Ask the Coach a question</h2> 
<fieldset> 
<form> 
@using (Html.BeginForm(FormMethod.Get)) 
{ 
    @Html.ValidationSummary() 
    <p>From: </p> 
    <p>@Html.Label("From") 

     @Html.TextBoxFor(m => m.From)</p> 

    <p>@Html.Label("To") 
     @Html.TextBoxFor(m => m.To)</p> 
    <p>@Html.Label("Subject") </p> 
    <p>@Html.TextBoxFor(m => m.Subject)</p> 
    <p>@Html.Label("Body")</p> 
    <p>@Html.TextAreaFor(m => m.Body)</p> 
    <input type="submit" value="Send" /> 
} 
</form> 
</fieldset> 

控制器:

public class SendMailerController : Controller 
{ 
    // 
    // GET: /SendMailer/ 

    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ViewResult Index(DonsSolution.Models.MailModel _objModelMail) 
    { 
     if (ModelState.IsValid) 
     { 
      MailMessage mail = new MailMessage(); 
      mail.To.Add(_objModelMail.To); 
      mail.From = new MailAddress("[email protected]gmail.com"); 
      mail.Subject = _objModelMail.Subject; 
      string Body = _objModelMail.Body; 
      mail.Body = Body; 
      mail.IsBodyHtml = true; 
      SmtpClient smtp = new SmtpClient(); 
      smtp.Host = "smtp.gmail.com"; 
      smtp.Port = 587; 
      smtp.UseDefaultCredentials = false; 
      smtp.Credentials = new System.Net.NetworkCredential 
      ("[email protected]", "password");// senders User name and password 
      smtp.EnableSsl = true; 
      smtp.Send(mail); 
      return View("Home", _objModelMail); 
     } 
     else 
     { 
      return View(); 
     } 

    } 

型號:

public class MailModel 
{ 
    public string From { get; set; } 
    public string To { get; set; } 
    public string Subject { get; set; } 
    public string Body { get; set; } 

} 
+1

你得到的錯誤是什麼? – Cris

+0

_「不確定我缺少什麼,因爲在IDE中沒有顯示錯誤。」_ - 編譯器錯誤是最無聊的一種。歡迎來到運行時,任何事情都可能發生。你的'smtp.Send()'命中嗎? – CodeCaster

回答

0

我建議設置啓用SSL爲假,看起來,你會得到這個

錯誤類型的異常「 System.Net.Mail.SmtpException'發生在System.dll中,但未在用戶代碼中處理

附加信息:SMTP服務器需要安全連接或客戶端未通過身份驗證。服務器響應是:5.7.0必須首先發出STARTTLS命令。 dj8sm48457278wid.2 - gsmtp。

您的代碼未創建安全連接。

相關問題