我知道对此还有更多问题,但它们似乎对我没有帮助。
我遵循了以下教程:https://www.youtube.com/watch?v=hlZexx2gbNs。
我想在MVC ASP.NET中发送电子邮件发送表单。 除了最后一步,一切似乎都可以正常工作。我必须更改RouteConfig,但没有routeconfig。
我想需要对此进行更改,但我不知道在哪里进行更改。有谁知道如何解决这个问题?
如果在另一个问题上有一个好的解决方案,请告诉我。
这是我用于视图的代码:
@media #{$mobile}, #{$tablet}
这是我用来发送邮件的代码:
@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();
}
}
答案 0 :(得分:0)
异常消息提到的问题很简单:您有两个具有相同名称和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) { ... }
由于您有BeginForm
个使用FormMethod.Post
的助手:
@using (Html.BeginForm("Index", "EmailSetup", FormMethod.Post, new { enctype = "multipart/form-data" }))
然后,您必须使用Index
属性用参数修饰第二个[HttpPost]
动作,以区别其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();
}