SMTP with Gmail and two step authentication
I am looking for a solution to get around this, the following code works, only if I turn on use less secure applications.
So the question is, how can I do this without switching this option on?
using (var message = new MailMessage())
{
message.To.Add(new MailAddress("ToEmail", "To Name"));
message.From = new MailAddress("FromEmail", "From Name");
message.Subject = "Subject";
message.Body = "Body";
message.IsBodyHtml = true;
using (var client = new SmtpClient("smtp.gmail.com"))
{
client.UseDefaultCredentials = false;
client.Port = 587;
client.Credentials = new NetworkCredential("username", "Password");
client.EnableSsl = true;
client.Send(message);
}
}
You can use app specific passwords with Gmail / Google Workspace (G Suite): https://support.google.com/accounts/answer/185833 https://myaccount.google.com/apppasswords
Tip: You will not be able to use "App passwords" if you are enrolled in Google's "Advanced protection" program - you must unenroll first if you want to use app specific passwords: https://landing.google.com/advancedprotection/
You can use app specific passwords with Gmail / Google Workspace (G Suite): https://support.google.com/accounts/answer/185833 https://myaccount.google.com/apppasswords
Tip: You will not be able to use "App passwords" if you are enrolled in Google's "Advanced protection" program - you must unenroll first if you want to use app specific passwords: https://landing.google.com/advancedprotection/
But that is not a solution I want to do either, this is obviously something to do with oAuth and I would prefer a working solution for that, rather than these type of hacks!
And I looked into the App Password stuff, and can not see how to do that with Google Apps.
If you don't want to use app passwords then you will need to create an option for users to sign into your app with their Google account: https://support.google.com/accounts/answer/112802?hl=en&ref_topic=7188760 https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins?view=aspnetcore-5.0
And I looked into the App Password stuff, and can not see how to do that with Google Apps.
My instructions above work for both normal gmail accounts and Google Apps / Workspace / G Suite or whatever they are calling it now.