Question

When I send mail from my Web page is both the from and to [personalemailremoved]@gmail.com when I received the email. What do I need to change to view from mail in gmail that the user filled in on the web site?

I Use Gmail to send email in my form and receive it via gmail:

Contact.cshtml

@using (Html.BeginForm("Contact", "Home", FormMethod.Post, new { id = "contactform" }))
    {

        <ol>
            <li>
                <label for="mail">
                    Din E-post*</label>
                @Html.TextBox("mail", null, new { @class = "text" })
            </li>
            <li>
                <label for="rubrik">
                    Rubrik*</label>
                @Html.TextBox("rubrik", null, new { @class = "text" })
            </li>
            <li>
                <label for="message">
                    Meddelande*</label>
                @Html.TextArea("meddelande")
            </li>
            <li class="buttons">
                <input type="submit" name="imageField" id="imageField" value="Skicka meddelande"
                    class="button" />
                <div class="clr">
                </div>
            </li>
        </ol>
    }

HomeController:

 public ActionResult Contact(string mail, string rubrik, string meddelande)
        {
            try
            {
                WebMail.SmtpServer = "smtp.gmail.com";
                WebMail.EnableSsl = true;
                WebMail.UserName = "personalemailremoved@gmail.com";
                WebMail.Password = "MYPASSWORD";
                WebMail.SmtpPort = 587;
                WebMail.Send(
                        "personalemailremoved@gmail.com",
                        rubrik,
                        meddelande,
                        mail
                    );

                return RedirectToAction("MailSent");
            }
            catch (Exception)
            {
                ViewData.ModelState.AddModelError("", "Kontrollera uppgifterna");
            }

            return View("Contact");
        }
        public ActionResult MailSent()
        {
            return View();
        }
Was it helpful?

Solution

Gmail actually changes this to the authenticated account automatically, so you need to use another server if you want to do that.

If you don't want to use a different SMTP server, don't use any at all! When you connect to the mail server, don't give it credentials. This is how SMTP servers send mail. It is important to note that this will only work if you are only sending an email to the user with a gmail.com email address. Otherwise it thinks you are using it as an open relay. You can have other addresses in the "To:" field, you just can't add them when doing the SMTP communication. I actually did an independent study on this exact thing in college, I still have my code. You may actually need to do the SMTP communication by hand. If you need a quick guide to SMTP spec I can post a link to the handout I made that explains it (though it is less than official, it should be easier to read than the RFC).

OTHER TIPS

relaying through gmail will always set the from as the authenticated user. for freedom to set the from address, you need to use a different smtp server.

You can use System.Net.Mail instead combined with ASP.NET's Mail Message class. Here is an example:

using System.Net.Mail;

// Create email code
MailMessage m = new MailMessage();

m.From = new MailAddress("address@gmail.com", "display name");
m.To.Add("address@gmail.com");
m.Subject = "subject";
m.Body = "Body";

SmtpClient smtp = new SmtpClient("Your SMTP Server");
smtp.Send(m);

Most smtp servers, do not accept a sender other as the authenticated user. Or from the domain they are supposed to be sending mail from.

There are some free smtp server, that relay mail from every sender, but they end up on some blacklist very soon.

The owner of a domain, can add some code to their DNS server, to indentify mail servers that may send email in their name. This can be checked by the receiver, and the receiver can use it to identify false senders.

As you can see, it is not very easy to adjust the "From" field in an email, and still make sure the mail arrives.

You might change the "reply to" field. But also this will make your "spam score" a little higher.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top