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 email code, but how to assign the Click of the button to my code in the Controller and also the fields. I am using SPA for learning, so I used the VS2015 template and I modified it as needed.
This is inha 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>
}
Here 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 template:
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 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 entered on the page by the users of the same.
[email protected] - > This is where I'm sending the email. I already changed (Inverti) and it continues without sending. With the above configuration, I get this error: