Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use ASP.NET to send email through a Google Apps account without selecting the 'Allow less secure apps' option?

I have an ASP.NET web API that I'm trying to use to send an email. I'm getting authentication issues using smtp.gmail.com and 587. I've seen several links saying to toggle the 'Allow less secure apps' option on my Google Apps account, but I'd rather not have to explain to the owners why they need to pick this dubious-sounding setting.

Is there a way to package an email request in ASP.NET that Google will like, or will I have to select the 'Allow less secure apps' option?

This is the code I'm using that Google doesn't like.

using (var client = new SmtpClient("smtp.googlemail.com", 587))
{
    client.Credentials = 
        new System.Net.NetworkCredential("[email protected]", "yourpassword");
    client.EnableSsl = true;
    var msg = new MailMessage("[email protected]", "[email protected]");
    msg.Body = "[YOUR EMAIL BODY HERE]";
    msg.Subject = "[Message Subject]";

    client.Send(msg);
}
like image 537
jvance Avatar asked Dec 25 '22 08:12

jvance


1 Answers

Google changed some time ago (one or two years ago) possibility to use its SMTP servers. Google now requires higher level of security and authentication. However there still is a possibility to use it without 'Allow less secure apps' option, but you cannot use your standard google username and password.

Here is how to do it (in short):

  1. Turn on 2-Step Verification in your gmail or google apps account in “My account / Sign-in & security”,
  2. then (in “My account / Sign-in & security / App passwords”) it is possible to generate a special password for access from other applications (without turned on 2-Step Verification you can not get there)
  3. this created app password (and your google e-mail account as a username) you can then use as your SMTP login.

That's all.

like image 76
David Najman Avatar answered Apr 19 '23 22:04

David Najman