9

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?

2
  • User / Password Auth has been deactivated for GMAIL, afaik. You need to switch to another auth method. I'll try and find a google documentation on that.
    – Fildor
    Jun 8, 2022 at 14:56
  • Have a look into this: support.google.com/a/answer/…
    – Fildor
    Jun 8, 2022 at 14:59

4 Answers 4

38

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

12
  • 1
    I did everything as described, but I still get an error :Failure sending mail. Unable to read data from the transport connection: The connection was closed.
    – Maria
    Jun 9, 2022 at 12:38
  • It seems to work for everyone except for me. I posted a separate question and people said they ran my code and it works. I tried it with another gmail account, set the 2step verification and app password, and I get a different error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required with status code System.Net.Mail.SmtpStatusCode.MustIssueStartTlsFirst. Are there any other conditions gmail must meet?
    – Maria
    Jun 10, 2022 at 7:38
  • @Maria Did you set EnableSsl to true? Did you try the script on my answer with this new account? A different error is already a step forward. Jun 10, 2022 at 7:47
  • I did, my code is practically the same as yours, which is why I think there's something wrong with the gmail account, but I have no idea what could be
    – Maria
    Jun 10, 2022 at 7:53
  • Are you using .net 6? I did the tests in .net 6. Jun 10, 2022 at 7:57
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 check your email and confirm you added app password.After confirming i was able to send email.

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

0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.