how can I change the email and name from the sender?

2.1k views Asked by At

I try to create a contact form in mvc 4, it's work but the sender (email and name) is always the same that I use in my credentials for the smtp, I want to display the email and the name of the user who send it from my contact form, any help? this is my code:

model:

[Required(ErrorMessage="Requerido")]
public string nombre { get; set; }

[Required(ErrorMessage = "Requerido")]
[DataType(DataType.EmailAddress)]
public string email { get; set; }

[Required(ErrorMessage = "Requerido")]
public string telefono { get; set; }

[Required(ErrorMessage = "Requerido")]
public string comentario { get; set; }

controller:

[HttpPost]
    public ActionResult Index(contactModel model)
    {
        if (ModelState.IsValid)
        {
            insertContact(model.nombre, model.email, model.telefono, model.comentario);
            TempData["notice"] = "your form was submitted.";
            return RedirectToAction("Index", "Home");
        }  

        return View();
    }

private void insertContact(string nombre, string email, string telefono, string comentario)
    {    
        MailMessage mail = new MailMessage();
        mail.To.Add("[email protected]");
        mail.From = new MailAddress(email, nombre);
        mail.Sender = new MailAddress(email, nombre);
        mail.Subject = "Solicito informacion";
        mail.Body = telefono + comentario;
        mail.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 25;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential("[email protected]","my.pass");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
1

There are 1 answers

0
TomTom On

I want to display the email and the name of the user who send it from my contact form, any help?

Yes. DO NOT DO THAT. Ever heard of spam? People sending emails in other domains accounts?

One way to fight that is SPF - a standard approah for any domain not run by someone who wants to be abused.

SPF tells for a domain which server is allowed to send emails.

So, what happens now is this:

  • I fill out your contact form.
  • Your web server sends the email to your email server. It is using my email as sender.
  • Your email server sees my domain, checks the SPF record and throws it away because your wqeb server is not allowed to send emails for my domain. This is a hard fail condition and snesible email server just throw confirmed spam away.

So much for contact. I Hope you hate contacts because gmail will happily throw away any information you get like that.

You need to whitelist that. But bad news - have fun trying to get gmail to whitelist your web server. I.e. not possible.

That simple.

Do not do it. Do not pretend to be someone else than you are when you want to use a public email server.