Reputation: 1
I have a doubt have a project done in ASP.NET MVC 3, where there is a contact form. It works perfectly the problem is that I want to put a message "Email Sent successfully!" to complete your submission. I'm not using Json. What would be the most appropriate solution Thank you!
the code:
Contato.cshtml `
@model SSInstitucional.ViewModel.ContatoViewModel
@using System.Web
<link href="../../Content/themes/SCTecno.css" rel="stylesheet" type="text/css" />
@using (Html.BeginForm("EnviaEmail"))
{
@Html.ValidationSummary(true, "")
<div id ="divContato" style="text-align:left; margin-left:460px; margin-right:400px; font-family:Verdana;">
<div id="txtNome" class="editor-label">
@Html.LabelFor(model => model.Nome)
<p>
@Html.TextBoxFor(m => m.Nome, new { id = "Nome", size = 40, maxlength = "60" })
@Html.ValidationMessageFor(model => model.Nome)
</div>
<div id="txtEmail" class="editor-field">
@Html.LabelFor(model => model.Email)
<p>
@Html.TextBoxFor(m => m.Email, new { id = "Email", size = 40, maxlength = "200" })
@Html.ValidationMessageFor(model => model.Email)
</div>
<div id="txtAssunto"class="editor-label">
@Html.LabelFor(model => model.Assunto)
<p>
@Html.TextBoxFor(m => m.Assunto, new { id = "assunto", size = 40, maxlength = "200" })
@Html.ValidationMessageFor(model => model.Assunto)
</div>
<div id="txtMensagem" class="editor-field">
@Html.LabelFor(model => model.Mensagem)
<p>
@Html.TextAreaFor(model => model.Mensagem, new { id = "mensagem", rows = "10", cols = "50", maxlength = "5000" })
@Html.ValidationMessageFor(model => model.Mensagem)
</div>
<div id="Enviar" onClick="alert('E-mail enviado com sucesso!!')")>
@SSHtml.SubmitStyledButton("Enviar")
</div>
</div>
}
`
ContatoController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SSInstitucional.ViewModel;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Diagnostics;
namespace SSInstitucional.Controllers
{
public class ContatoController : Controller
{
//
// GET: /Contato/
public ActionResult Index()
{
return View();
Debug.WriteLine("Debug Teste");
}
[HttpPost]
public ActionResult Index(ContatoViewModel contatoViewModel)
{
string corpo = "Nome: " + contatoViewModel.Nome + "\n" +
"E-Mail: " + contatoViewModel.Email + "\n" +
"Assunto: " + contatoViewModel.Assunto + "\n\n" +
contatoViewModel.Mensagem;
sendEmail(contatoViewModel.Nome, contatoViewModel.Email, contatoViewModel.Assunto, corpo);
// return Json ( new { Sucess = true, MessageBox = "Email enviado com sucesso!" });
// return Json (new { mbox = "E-Mail enviado com sucesso!" });
return View();
}
public ActionResult EnviaEmail()
{
return View();
}
private void sendEmail(string fromName, string FromEmail, string subject, string body)
{
try
{
string smtpEmail = null;
string usuarioEmail = null;
string senhaEmail = null;
int smtpPort = 0;
bool enableSsl = false;
MailMessage mail = new MailMessage();
////set the addresses
mail.From = new MailAddress(FromEmail, fromName);
mail.Sender = new MailAddress(FromEmail, fromName);
mail.ReplyTo = new MailAddress(FromEmail, fromName);
mail.To.Add(new MailAddress("[email protected]", fromName));
//set the content
mail.Subject = subject;
mail.Body = body;
mail.Body += "\n\n------------------------------------------";
mail.Body += "\n\nEmail de origem: " + FromEmail;
mail.Body += "\n\n\nEmail enviado pelo Fale Conosco do site.";
//send the message
//SmtpClient smtp = new SmtpClient(smtpEmail);
SmtpClient smtp = new SmtpClient("smtp.webmail.net");
NetworkCredential credenciais = new NetworkCredential("[email protected]", "sss1122");
smtp.Credentials = credenciais;
//smtp.Port = 587;
if (smtpPort > 0)
smtp.Port = 25;
// enable SSL
if (enableSsl)
smtp.EnableSsl = true;
smtp.Timeout = 120000;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
{
ModelState.Clear();
//return mail(new { Message = "E-mail enviado com sucesso!" });
}
}
catch (FormatException erroFormato)
{
throw new Exception("Falha ao enviar email. Verifique se seu email foi digitado corretamente.");
}
catch (Exception erro)
{
throw new Exception("Falha ao enviar email.");
}
}
}
}
Upvotes: 0
Views: 1205
Reputation: 1211
If you can use js, you could use a MVC Ajax form. A example is here http://www.hanselman.com/blog/ASPNETMVCPreview4UsingAjaxAndAjaxForm.aspx
You just need replace
@using (Html.BeginForm("EnviaEmail"))
{
....
}
with
@using(Ajax.BeginForm("EnviaEmail", new AjaxOptions
{
UpdateTargetId = "form_wrapper_id",
InsertionMode = InsertionMode.Replace
}))
{
...
}
and return from Action some massage. This message will be put to HTML tag with id 'form_wrapper_id'. If you can't use js the answer by Darin Dimitrov is the best.
Upvotes: 2
Reputation: 1039498
Your form is currently pointing to the EnviaEmail
controller action. The EnviaEmail
controller action doesn't do anything. It only redisplays the view. It doesn't send any email whatsoever. So I guess that your original code is actually pointing to the Index POST action:
@using (Html.BeginForm())
{
...
}
From what I can see in your code this POST action sends an email and redisplays the same view. So once the email is sent you could store some message in the ViewBag:
[HttpPost]
public ActionResult Index(ContatoViewModel contatoViewModel)
{
string corpo = "Nome: " + contatoViewModel.Nome + "\n" +
"E-Mail: " + contatoViewModel.Email + "\n" +
"Assunto: " + contatoViewModel.Assunto + "\n\n" +
contatoViewModel.Mensagem;
sendEmail(contatoViewModel.Nome, contatoViewModel.Email, contatoViewModel.Assunto, corpo);
ViewBag.Message = "Email sent successfully";
return View();
}
and then in your view display this message:
<div>@ViewBag.Message</div>
Upvotes: 0