4

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:

AuthenticationException Error Page

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>
                }
Nebih Başaran
  • 101
  • 2
  • 12

2 Answers2

5

Ensure you`ve enabled access to your account for "unreliable" sources: https://myaccount.google.com/lesssecureapps

-1

Have you tried following the directions given in the error message? It says you need to login via a web browser first. This is a security feature of GMail.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • But I'm going to publish this web application to public. Will everyone who try to send a message have to be signed in to my email address with this logic? – Nebih Başaran Oct 13 '17 at 15:36
  • No, just your web server will need to login to your gmail account. – jstedfast Oct 13 '17 at 15:37
  • How do I login to my account in my webserver though? I'm using a third party hosting service and I'm using gsuite for my e-mails. – Nebih Başaran Oct 13 '17 at 16:59
  • Depends on your hosting provider. Maybe you can remote login and open a web browser on your host and login to gmail view the web that way? I don't know. – jstedfast Oct 13 '17 at 17:32