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();
}
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).