[ACCEPTED]-Unable to send email in C# Less secure app access not longer available-winforms

Accepted answer
Score: 39

The deactivation of less secure applications 11 prevents you from being able to log in directly 10 with your username and password, but it does not prevent 9 you from being able to generate a specific 8 password for your application. Now, instead 7 of logging in with your google password, you'll 6 log in with a password that you generate 5 for your specific app.

The solution is simple 4 and does not require much change:

  1. Turn on 2-Step Verification in your google account. This step is required as Google only allows generating passwords for apps on accounts that have 2-Step Verification enabled.
  2. Go to generate apps password (https://myaccount.google.com/apppasswords) and generate a password for your app.

  1. Simply use your gmail username (your_mail@gmail.com) and the password generated in your c# application.

I have 3 tested that it works with a small console 2 application that I am attaching below:

using System.Net;
using System.Net.Mail;

string username = "your_mail@gmail.com";
string password = "generated_password";
ICredentialsByHost credentials = new NetworkCredential(username, password);

SmtpClient smtpClient = new SmtpClient()
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    Credentials = credentials
};

MailMessage mail = new MailMessage();
mail.From = new MailAddress(username);
mail.To.Add(username);
mail.Subject = "Testing less secure apps new configuration.";
mail.Body = "Hello stackoveflow!";

smtpClient.Send(mail);

And 1 it works perfectly:

enter image description here

Score: 1

If you get this error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required

You may also want 3 check your email and confirm you added app 2 password.After confirming i was able to 1 send email.

https://i.stack.imgur.com/iswPV.png

Score: 0

For starters, don't use SmtpClient. That 12 class is obsolete and Microsoft itself advises against its use in the documentation. SmptClient 11 simply doesn't support newer protocols, much 10 less authentication protocols like OAuth. The 9 proposed alternative is to use MailKit

MailKit can connect 8 both in a less secure mode or by using OAuth. Connecting 7 to GMail in general is described in the 6 FAQ.

The doc page Using OAuth2 With GMail (IMAP, POP3 or SMTP) shows how to create a Google 5 API project, configure it for GMail access 4 and OAuth authentication and finally, shows 3 how to send emails. The doc page shows how 2 to handle authentication for both desktop 1 and web applications

Score: 0

Dealt with this today. Just go to the gmail 12 account, then go to Manage Your Google Account 11 > Security.

From here enable 2-factor 10 authentication, then once you have done 9 You will see the "App passwords" option 8 appear under the 2-step verification option. Click 7 on this, name the device that you want to 6 use, and then copy & paste the generated 5 password that you are given into your code 4 in place of the old password that you were 3 using.

I've done this now for our office 2 printer & the python script that I had 1 to automatically deliver timesheets to everyone.

More Related questions