在 ASP MVC 中发送邮件有服务器错误

Send mail in ASP MVC have Server Error

我在教程中练习使用 ASP MVC 发送电子邮件时遇到问题: How To Send Email In ASP.NET MVC

我认为 class 中的问题:

public async Task<<ActionResult>ActionResult> Index(EmailFormModel model)
{
  todosomething
  return RedirectToAction("Sent");
}

当 运行 网站显示以下错误:

求解: 感谢@Simon C。 因此,根据您的评论,您有一个 POST 联系方法,但还无法导航到该页面(只需在浏览器中导航到 home/contact 将发送 GET 请求,因为您有一个 [HttpPost] 属性,路由将不匹配)。我认为您需要向家庭控制器添加 GET 方法才能到达那里:

[HttpGet]
public ActionResult Contact()
{
    return View(new EmailFormModel());
}

这意味着当你第一次导航到/home/contact时,你会点击上面的方法。当您 post 您的表单返回您的站点时,您将点击标有 [HttpPost] 的联系方式。

编辑:顺便说一句,您还需要在控制器上添加一个名为 Sent 的方法,以便在成功发送电子邮件时使用。该行 return RedirectToAction("Sent");将寻找一种名为 Sent.

的方法

在EmailFormModel.cs型号中:

using System.ComponentModel.DataAnnotations;
using System.Web;
namespace MVCEmail.Models
{
    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; }
        public HttpPostedFileBase Upload { get; set; }
    }
}


将 HomeController.cs 中的 class 联系人更改为:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Contact(EmailFormModel model)
{
    if (ModelState.IsValid)
    {
        var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
        var message = new MailMessage();
        message.To.Add(new MailAddress("name@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;
        if (model.Upload != null && model.Upload.ContentLength > 0)
        {
            message.Attachments.Add(new Attachment(model.Upload.InputStream, Path.GetFileName(model.Upload.FileName)));
        }
        using (var smtp = new SmtpClient())
        {
            await smtp.SendMailAsync(message);
            return RedirectToAction("Sent");
        }
    }
    return View(model);
}

并更改联系人视图 (Views\Home\Contact.cshtml):

@model MVCEmail.Models.EmailFormModel
@{
    ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>

@using (Html.BeginForm("Contact", "Home", null, FormMethod.Post, new {enctype = "multipart/form-data"}))
{
    @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">
        @Html.LabelFor(m => m.Upload, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            <input type="file" name="upload" />
        </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")
}

而Webconfig.cs是:

<system.net>
  <mailSettings>
    <smtp from="you@outlook.com">
      <network host="smtp-mail.outlook.com" 
               port="587" 
               userName="you@outlook.com"
               password="password" 
               enableSsl="true" />
    </smtp>
  </mailSettings>
</system.net>

感谢@Dijkgraaf

因此,根据您的评论,您有一个 POST 联系方法,但还无法导航到该页面(只需在浏览器中导航到 home/contact 就会发送 GET 请求,并且因为你有一个 [HttpPost] 属性,路由将不匹配)。我认为您需要向家庭控制器添加 GET 方法才能到达那里:

    [HttpGet]
    public ActionResult Contact()
    {
        return View(new EmailFormModel());
    }

这意味着当你第一次导航到/home/contact时,你会点击上面的方法。当您 post 将表单返回到您的站点时,您将点击标有 [HttpPost] 的联系方式。

编辑:顺便说一句,您还需要在控制器上添加一个名为 Sent 的方法,以便在成功发送电子邮件时使用。 return RedirectToAction("Sent"); 行将查找名为 Sent 的方法。