Unable to send email in C# Less secure app access not longer available

Viewed 4906

I have an winform application running on our production floor and it sends email for reporting, so since yesterday its unable to send emails and i got this message

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required."

I checked this post The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required?

and I found that google is not longer supporting 3rd party app, it doesn't allow less secure apps this is from google less secure app Less secure app access:

Some apps and devices use less secure sign-in technology, which makes your account vulnerable. You can turn off access for these apps, which we recommend, or turn it on if you want to use them despite the risks. Google will automatically turn this setting OFF if it’s not being used. This setting is no longer available. Learn more

so I have tried adding SmtpServer.UseDefaultCredentials = false; but nothing works, I think the issue is google that is not longer supporting 3rd party access to email. This is my code

try
{
    MailMessage mail = new MailMessage();
    System.Net.Mail.SmtpClient SmtpServer =
        new System.Net.Mail.SmtpClient("smtp.gmail.com");
    string sender = "user@gmail.com";
    mail.From = new MailAddress(sender);
    mail.To.Add("receiver@plastikon.com");
    mail.Priority = MailPriority.High;
    mail.Subject = subject;
    mail.IsBodyHtml = true;
    mail.Body = ($"{body} \n Name of computer: { HostName} ");

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new 
    System.Net.NetworkCredential("user@gmail.com", "Password");
    SmtpServer.EnableSsl = true;
    SmtpServer.UseDefaultCredentials = false;

    SmtpServer.Send(mail);
}

The question is: is there a solution for this or does anyone can recommend me another way to send emails or an API or something?

4 Answers

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

The solution is simple 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 tested that it works with a small console 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 it works perfectly:

enter image description here

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 check your email and confirm you added app password.After confirming i was able to send email.

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

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

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

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

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

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

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

Related