1
I need to do a routine to send email, through a form I made, using the VS2015 SPA Template. I put two buttons, Cancel and Send. The question is not in the code of the email, but how to assign the Click of the button to my code in the Controller and also the fields. I’m using SPA for learning, so I used the VS2015 template and changed according to my needs.
This is Little View(CSHTML)
@model Projeto.Models.ContactEmail
@using (Html.BeginForm("Email", "Email", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Enviar email.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Nome, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Nome, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Assunto, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Assunto, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Texto, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextAreaFor(m => m.Texto, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-danger" value="Cancelar" />
<input type="submit" class="btn btn-success" value="Enviar" />
</div>
</div>
}
This is the Controller code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
namespace Projeto.Controllers
{
public class EmailController : Controller
{
// GET: Email
public ActionResult Email()
{
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> EnviarEmail()
{
//Aqui deve ir o código de tudo, enviar email e etc
return View(model);
}
}
}
This is the Email model:
public class ContactEmail
{
[Required]
[Display(Name = "Nome")]
public string Nome { get; set; }
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[Display(Name = "Assunto")]
public string Assunto { get; set; }
[Required]
[Display(Name = "Texto")]
public string Texto { get; set; }
}
This is my email sending routine:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult EnviarEmail(ContactEmail contactEmail)
{
WebMail.SmtpServer = "mail19.redehost.com.br";
WebMail.SmtpPort = 587;
//WebMail.EnableSsl = true;
WebMail.From = contactEmail;
WebMail.UserName = "[email protected]";
WebMail.Password = "tested123";
WebMail.Send(contactEmail.Email, contactEmail.Assunto, contactEmail.Texto);
return View(contactEmail);
}
Where contactEmail
is the email typed on the page by the users of it.
[email protected] -> That’s where I’m sending the email. I already switched it (Inverti) and still can’t send it. With the above configuration, it gives me this error:
What the cancel button should do?
– Randrade
@Randrade, he would cancel the shipment, clearing the fields.
– pnet
I picked up a reply from the Gypsy in another thread and managed to resolve the issue of the "conversation" between Controller and View(Razor). It helped me solve the question.
– pnet
Couldn’t you just clear the fields or refresh the page? A simple button would do what you want
<a href="@Url.Action("Email", "Email")" class="btn btn-danger"> Cancelar</a>
. If you’re not gonna get any action, I don’t see the need to make aPOST
for that reason.– Randrade
@Randrade, your comment above solved the question of clearing the form.
– pnet
Alter your
form
for@using (Html.BeginForm("EnviarEmail", "Email", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
. Note that you are going toAction
Email, but the name in your controller is Email.– Randrade