I am trying to have the user fill out a suggestion form. The form is meant to be sent to my admin's email address. Not receiving any emails. This is my view. I am a beginner at programming. Unsure what I am missing since there are no errors displaying in the IDE.
@model DonsSolution.Models.MailModel
<h2>Ask the Coach a question</h2>
<fieldset>
<form>
@using (Html.BeginForm(FormMethod.Get))
{
@Html.ValidationSummary()
<p>From: </p>
<p>@Html.Label("From")
@Html.TextBoxFor(m => m.From)</p>
<p>@Html.Label("To")
@Html.TextBoxFor(m => m.To)</p>
<p>@Html.Label("Subject") </p>
<p>@Html.TextBoxFor(m => m.Subject)</p>
<p>@Html.Label("Body")</p>
<p>@Html.TextAreaFor(m => m.Body)</p>
<input type="submit" value="Send" />
}
</form>
</fieldset>
Controller:
public class SendMailerController : Controller
{
//
// GET: /SendMailer/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ViewResult Index(DonsSolution.Models.MailModel _objModelMail)
{
if (ModelState.IsValid)
{
MailMessage mail = new MailMessage();
mail.To.Add(_objModelMail.To);
mail.From = new MailAddress("example@gmail.com");
mail.Subject = _objModelMail.Subject;
string Body = _objModelMail.Body;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("example@gmail.com", "password");// senders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
return View("Home", _objModelMail);
}
else
{
return View();
}
}
Model:
public class MailModel
{
public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
I would suggest setting enable SSL to false, looks like you will getting this
error An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. dj8sm48457278wid.2 - gsmtp.
you code is not creating a secure connection.