Well, I have an asp.net mvc application that will have only one page, which is index.cshtml. On this page I have a contact form, so he is not sending the message he has received to my email, someone would know how to help me solve this, here is my code below.
Index.cshtml - Here is my form
<div class="w3-col m6">
<form action="@Url.Action("Index", "Home")" method="post">
<div class="w3-row-padding" style="margin:0 -16px 8px -16px">
<div class="w3-half">
<div class="form-group">
<label for="exampleSelect1">Você aceita essa proposta?</label>
<select class="form-control" id="exampleSelect1">
<option>Sim</option>
<option>Não</option>
</select>
</div>
</div>
</div>
<input class="w3-input w3-border" type="text" placeholder="Deseja dizer algo?" required name="Mensagem">
<button class="w3-button w3-black w3-section w3-right" type="submit">ENVIAR</button>
</form>
</div>
HomeController.cs - Here is the logical part that will take care of smtp
public class HomeController : Controller
{
// GET: Home
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string exampleSelect1, string Mensagem)
{
//Faça validação adicional nos seus parâmetros de entrada
if (!string.IsNullOrWhiteSpace(exampleSelect1) && !string.IsNullOrWhiteSpace(Mensagem))
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "email-ssl.com.br ";
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "minha_senha");
MailMessage mail = new MailMessage();
mail.Sender = new System.Net.Mail.MailAddress("[email protected]");
mail.From = new MailAddress("Orçamentos");
mail.To.Add(new MailAddress("[email protected]", "RECEBEDOR"));
mail.Subject = "Contato";
mail.Body = "Nome: " + exampleSelect1 + " <br/> Mensagem : " + Mensagem;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
try
{
client.Send(mail);
}
catch (System.Exception erro)
{
//trata erro
}
finally
{
mail = null;
}
}
//Vai retornar para sua Contatos com o verbo [HttpGet]
return View();
}
}