FluentEmail icon indicating copy to clipboard operation
FluentEmail copied to clipboard

SMTP with Gmail and two step authentication

Open cyberangel67 opened this issue 4 years ago • 3 comments

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);
            }
        }

cyberangel67 avatar Feb 12 '21 04:02 cyberangel67

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/

JustCallMeAD avatar Mar 13 '21 16:03 JustCallMeAD

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.

cyberangel67 avatar Mar 13 '21 17:03 cyberangel67

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.

JustCallMeAD avatar Mar 13 '21 18:03 JustCallMeAD