我知道对此还有更多问题,但它们似乎对我不起作用。
我遵循以下教程:https://www.youtube.com/watch?v=hlzexxx2gbns。
我想在MVC ASP.NET中发出电子邮件发送表格。除最后一步外,一切似乎都起作用。我不得不更改RouteConfig,但我没有RouteConfig。
我认为我需要对此进行更改以使其正常工作,但我不知道在哪里可以更改此问题。有人知道如何解决这个问题吗?
如果在另一个问题上有一个很好的解决方案,请告诉我。
这是我用于视图的DE代码:
@model smoothboard.Models.gmail
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<h4>Email</h4>
<hr />
<div>
@using (Html.BeginForm("Index", "EmailSetup", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td>
Naar:
</td>
<td>
@Html.TextBoxFor(gmail=>gmail.To)
</td>
</tr>
<tr>
<td>
<input type="submit" value="Verzend">
</td>
</tr>
</table>
}
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
这是我用来发送邮件的代码:
public class EmailSetupController : Controller
{
public IActionResult Index()
{
return View();
}
string from = new string("hans31833@gmail.com");
string subject = new string("Geregistreerd");
string body = new string("Je hebt je ingeschreven voor de nieuwsbrief!");
public IActionResult Index(smoothboard.Models.gmail gmail, string from)
{
MailMessage mm = new MailMessage(from, gmail.To);
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
NetworkCredential nc = new NetworkCredential("hans31833@gmail.com", "hans1234hans");
smtp.UseDefaultCredentials = true;
smtp.Credentials = nc;
smtp.Send(mm);
ViewBag.MailMessage = "Mail is verzonden";
return View();
}
}
这是我用于模型的方法:
namespace smoothboard.Models
{
public class gmail
{
public string To { get; set; }
}
}
异常消息提到的问题很简单:您有2个具有相同名称和相同HTTP方法的操作方法(默认情况下,HTTP方法是GET
,它隐含地使用[HttpGet]
):
// first action with GET method
public IActionResult Index() { ... }
// ambiguous matching here because this also uses GET method
public IActionResult Index(smoothboard.Models.gmail gmail, string from) { ... }
由于您有使用FormMethod.Post
的BeginForm
助手:
@using (Html.BeginForm("Index", "EmailSetup", FormMethod.Post, new { enctype = "multipart/form-data" }))
然后,您必须使用[HttpPost]
属性来装饰第二个Index
操作,以区分其HTTP方法:
[HttpPost]
public IActionResult Index(smoothboard.Models.gmail gmail, string from)
{
MailMessage mm = new MailMessage(from, gmail.To);
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
NetworkCredential nc = new NetworkCredential("hans31833@gmail.com", "hans1234hans");
smtp.UseDefaultCredentials = true;
smtp.Credentials = nc;
smtp.Send(mm);
ViewBag.MailMessage = "Mail is verzonden";
return View();
}