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....................