我在C_中创建了一个新的MVC5项目。
我有一些模型:
public class EmailFormModel
{
[Required, Display(Name = "Your name")]
public string FromName { get; set; }
[Required, Display(Name = "Your email"), EmailAddress]
public string FromEmail { get; set; }
[Required]
public string Message { get; set; }
}
我有一个conctact.cshtml:
@model EMailSenderWebApp.Models.EmailFormModel
@{
ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>
@using (Html.BeginForm("SendEmailAsync", "Home", FormMethod.Post, new { role = "form contact-form", @id = "form-div" }))
{
@Html.AntiForgeryToken()
<h4>Send your comments.</h4>
<hr />
<div class="form-group">
@Html.LabelFor(m => m.FromName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.FromName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FromName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.FromEmail, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.FromEmail, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FromEmail)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Message, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextAreaFor(m => m.Message, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Message)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Send" />
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
因此,如您所见,我的表单调用httppost方法“sendmailasync”,如我的主控制器中所述:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SendEmailAsync(EmailFormModel model)
{
if (ModelState.IsValid)
{
SmtpClient client = new SmtpClient();
MailMessage message = new MailMessage();
var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
message.To.Add(new MailAddress("stefan.cv5@gmail.com")); // replace with valid value
message.From = new MailAddress("stefan.cv5@gmail.com"); // replace with valid value
message.Subject = "Your email subject";
message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
message.IsBodyHtml = true;
client.Credentials = new NetworkCredential("", "");
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = false;
client.SendCompleted += (s, e) =>
{
client.Dispose();
message.Dispose();
};
ThreadPool.QueueUserWorkItem(o =>
client.SendAsync(message, Tuple.Create(client, message)));
}
return View(model);
}
我试过一些例子
等待客户端。SendMailSync()
但正如您可能知道的,异步调用不存在,并且永远不会返回(关于这个的更多信息
here
)
所以,我已经这样实现了
this stack-overflow article
这会一直执行方法(在最后返回视图),但我的电子邮件永远不会进入我的收件箱?
也许谷歌有防火墙?