2015-06-03 79 views
0

我做了一個聯繫表格,它沒有錯誤,但它沒有發送電子郵件這是我的網頁的主要目的?我使用SMTP gmail發送電子郵件。密碼隱藏在此代碼中,請在嘗試之前更改密碼。我很困惑,爲什麼它不工作。我嘗試了很多,但都是徒勞的。我在MVC 5工作,我認爲有問題。不是通過SMTP在asp.net中的聯繫表單發送電子郵件?

   //Model class code starts........................................... 

      using System; 
      using System.Collections.Generic; 
      using System.Linq; 
      using System.Web; 
      using System.Text; 
      using System.ComponentModel.DataAnnotations; 

      namespace Contacts.Models 
      { 
       public class Contact 
       { 
        [Required] 

        [Display(Name = "Name *")] 

        public string Name { get; set; } 

        [Required] 

        [DataType(DataType.EmailAddress)] 

        [Display(Name = "Email address *")] 

        public string Email { get; set; } 

        [DataType(DataType.PhoneNumber)] 

        [Display(Name = "Phone Number")] 

        public string Phone { get; set; } 

        [Required] 

        [Display(Name = "Message *")] 

        public string Body { get; set; } 

        public DateTime SentDate { get; set; } 

        public string IP { get; set; } 



        public string BuildMessage() 
        { 

         var message = new StringBuilder(); 

         message.AppendFormat("Date: {0:yyyy-MM-dd hh:mm}\n", SentDate); 

         message.AppendFormat("Email from: {0}\n", Name); 

         message.AppendFormat("Email: {0}\n", Email); 

         message.AppendFormat("Phone: {0}\n", Phone); 

         message.AppendFormat("IP: {0}\n", IP); 

         message.AppendFormat("Message: {0}\n", Body); 

         return message.ToString(); 

        } 
       } 
      } 

      Model class code end ............................. 

      Controller class code starts ............................. 

      using System; 
      using System.Collections.Generic; 
      using System.Linq; 
      using System.Web; 
      using System.Web.Mvc; 
      using Contacts.Models; 
      using System.Net; 
      using System.Net.Mail; 
      using System.Collections; 



      namespace Contacts.Controllers 
      { 
       public class ContactController : Controller 
       { 
        public ActionResult Index() 
        { 

         ViewBag.Success = false; 

         return View(new Contact()); 

        } 

        [HttpPost] 

        public ActionResult Index(Contact contact) 
        { 

         ViewBag.Success = false; 

         { 
          try 
          { 

           if (ModelState.IsValid) 
           { 

            // Collect additional data; 

            contact.SentDate = DateTime.Now; 

            contact.IP = Request.UserHostAddress; 



            SmtpClient smtpClient = new SmtpClient(); 
            smtpClient.UseDefaultCredentials = false; 
            smtpClient.Credentials = new System.Net.NetworkCredential 
            ("[email protected]", "********"); 

      smtpClient.EnableSsl = true; 

            MailMessage mail = new MailMessage(); 
            mail.From = new MailAddress("[email protected]"); // From 
            mail.To.Add(new MailAddress("[email protected]")); // To 

            mail.Subject = "Your email subject"; // Subject 
            mail.Body = contact.BuildMessage(); 



            contact.BuildMessage(); // Body 
            ViewBag.Success = true; 
            smtpClient.Send(mail); 
           } 

          } 

          catch (Exception e) 
          { 
           Response.Write("Success"); 

          } 
         } 



         return View(); 

        } 

       } 
      } 
      Controller class code ends ............................. 

      View class code Starts............................. 

      @model Contacts.Models.Contact 

      @{ 

       ViewBag.Title = "Contact Page"; 

      } 

      <h2> Contact Page</h2> 

      <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 

      <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> 

      @Html.ValidationSummary(true, "Sending message was unsuccessful. Please correct the errors and try again.") 

      @if (ViewBag.Success) 
      { 

       <h3>Your message was successfully sent!</h3> 

      } 

      @using (Html.BeginForm()) 
      { 

       <div> 

        <fieldset> 

         <legend>Contact Information</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.Email) 

         </div> 

         <div class="editor-field"> 

          @Html.TextBoxFor(model => model.Email) 

          @Html.ValidationMessageFor(model => model.Email) 

         </div> 

         <div class="editor-label"> 

          @Html.LabelFor(model => model.Phone) 

         </div> 

         <div class="editor-field"> 

          @Html.TextBoxFor(model => model.Phone) 

          @Html.ValidationMessageFor(model => model.Phone) 

         </div> 

         <div class="editor-label"> 

          @Html.LabelFor(model => model.Body) 

         </div> 

         <div class="editor-field"> 

          @Html.ValidationMessageFor(model => model.Body) 

          <br /> 

          <textarea rows="10" cols="60" name="Body"></textarea> 

         </div> 

         <p> 

          <input type="submit" value="Send" /> 

         </p> 

        </fieldset> 

       </div> 

      } 
      //View class ends.................... 

回答

0

這是一個處理smtp和gmail的很好的鏈接。

Sending email through Gmail SMTP server with C#

不管怎樣,我有你的代碼工作。你的MVC代碼中有幾個小錯誤。

在你的控制器,在catch塊,你有

catch (Exception e) 
    { 
     Response.Write("Success"); 
    } 

更改爲

catch (Exception e) 
    { 
     ModelState.AddModelError("",e.Message); 

    } 

這增加了從Gmail接收回來的錯誤模型的狀態。 catch塊捕獲錯誤,不成功!

那麼在你看來,你有下面的代碼行

@Html.ValidationSummary(true, "Sending message was unsuccessful. Please correct the errors and try again.") 

更改爲:

@Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

這將顯示錯誤消息,您將在catch塊,如果傳輸趕上失敗。

現在,去你的控制器之前,下面一行

smtpClient.Send(mail) 

,並添加以下代碼

smtpClient.Host = "smtp.gmail.com"; 
    smtpClient.Port = 587; 
    smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
    smtpClient.Timeout = 20000; 

,使它看起來像這樣

smtpClient.Host = "smtp.gmail.com"; 
    smtpClient.Port = 587; 
    smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
    smtpClient.Timeout = 20000; 
    smtpClient.Send(mail) 

它應該工作。如果你開始超時,那麼只需增加代碼中的超時時間即可。

相關問題