I know there have been more questions about this, but they dont seem to work for me.
I followed the following tutorial: https://www.youtube.com/watch?v=hlZexx2gbNs.
I want to make an email send form in MVC ASP.NET. Everything in seemed to work, except for the last step. I had to change the RouteConfig, but I dont have a routeconfig.
I think I need to change this for this to work, but I dont know where to change this. Does anyone have any idea how to solve this issue?
If there is a good solution for this on another question, let me know.
This is de code I use for the view:
@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>
This is the code I use to send the mail:
public class EmailSetupController : Controller
{
public IActionResult Index()
{
return View();
}
string from = new string("[email protected]");
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("[email protected]", "hans1234hans");
smtp.UseDefaultCredentials = true;
smtp.Credentials = nc;
smtp.Send(mm);
ViewBag.MailMessage = "Mail is verzonden";
return View();
}
}
This is what I use for my model:
namespace smoothboard.Models
{
public class gmail
{
public string To { get; set; }
}
}
The problem mentioned by exception message is straightforward: you have 2 action methods with same name and same HTTP method (by default HTTP method is GET
, which implicitly uses [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) { ... }
Since you have BeginForm
helper which uses FormMethod.Post
:
@using (Html.BeginForm("Index", "EmailSetup", FormMethod.Post, new { enctype = "multipart/form-data" }))
Then you must decorate the second Index
action with parameters using [HttpPost]
attribute to distinguish its HTTP method:
[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("[email protected]", "hans1234hans");
smtp.UseDefaultCredentials = true;
smtp.Credentials = nc;
smtp.Send(mm);
ViewBag.MailMessage = "Mail is verzonden";
return View();
}