I am using MVC ASP.NET Core 1.1.2 on a web application. I try to create the contact form using MailKit 1.18.1.1. I send the contact forms info via email (gmail). When I build the web page locally, everything works as expected. First time I ran the program locally, it gave me the same error but I enabled the security settings of my mail address and it started working. But when I deploy it on the web, It gives me an error like this even though I set the security settings to enable less secure apps:
Here is my SendMail method in the Home Controller:
public IActionResult SendMail(string name, string phone, string email, string msg)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress ("theemailaddress@gmail.com"));
message.To.Add(new MailboxAddress("theemailaddresswhichrecievestheinfo@gmail.com"));
message.Subject = name;
message.Body = new TextPart("html")
{
Text = "Kimden: " + name + "<br>" + "Mail adresi: " + email + "<br>" +
"Mesaj: " + msg + "<br>" + "Telefon: " + phone
};
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587);
client.Authenticate("theemailaddress@gmail.com", "thepassword(I am sure it's correct)");
client.Send(message);
client.Disconnect(false);
}
return View("Contact");
}
Here is my Contact.cshtml code for the form:
@using (Html.BeginForm("SendMail", "Home"))
{
<div class="contactForm">
<form action="" method="POST" role="form" class="form">
<div class="form-group">
<input name ="name" type="text" class="form-control" id="" placeholder="Adın">
</div>
<div class="form-group">
<input name ="email" type="email" class="form-control" id="" placeholder="Mail Adresin">
</div>
<div class="form-group">
<input name="phone" type="text" class="form-control" id="" placeholder="Telefonun">
</div>
<div class="form-group">
<textarea name="msg" class="form-control" id="" placeholder="Mesajın"></textarea>
</div>
<button type="submit" class="btn buttonCustomPrimary">Mesaj gönder</button>
</form>
</div>
}