【发布时间】:2021-02-16 12:28:16
【问题描述】:
我正在尝试在我的网页中创建“联系我们”表单,我尝试了一些方法,我认为它应该可以工作,但我仍然收到错误。
我试过这个:
HomeController.cs
public class HomeController : Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Contact(ContactModel model, string email, string subject, string htmlMessage)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress("");
mailMessage.Subject = subject;
mailMessage.Body = htmlMessage;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(email));
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp-mail.outlook.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "myemail@hotmail.com";
NetworkCred.Password = "myEmailPassword";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
await smtp.SendMailAsync(mailMessage);
}
return View(model);
}
public ActionResult SuccessMessage()
{
return View();
}
}
ContactModel.cs
public class ContactModel
{
public string Email { get; set; }
public string Name { get; set; }
public string Message { get; set; }
}
Contact.csshtml
@model ContactForSpacess.Models.ContactModel
<h2>ContactMail</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Contact</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
我收到以下错误: 应用程序中的服务器错误。找不到资源。 说明:HTTP 404。
感谢我能得到的所有想法和帮助!
谢谢。
【问题讨论】:
-
试试:
@using (Html.BeginForm("Contact", "Home", FormMethod.Post))
标签: c# asp.net email server-error