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?

ThomasArdal
  • 4,999
  • 4
  • 33
  • 73
hugodavidx
  • 93
  • 1
  • 1
  • 4
  • 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 08 '22 at 14:56
  • Have a look into this: https://support.google.com/a/answer/176600?hl=en#zippy=%2Cuse-the-gmail-smtp-server – Fildor Jun 08 '22 at 14:59

4 Answers4

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

Jaime Menendez
  • 1,323
  • 1
  • 9
  • 13
  • 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 09 '22 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 '22 at 07: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. – Jaime Menendez Jun 10 '22 at 07: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 '22 at 07:53
  • Are you using .net 6? I did the tests in .net 6. – Jaime Menendez Jun 10 '22 at 07:57
  • Yes, I'm currently testing with .net 6 – Maria Jun 10 '22 at 08:04
  • @Maria I really can't imagine what could be happening. Although this last error you show seems to be about the credentials (the first one was more about connection.). Just to confirm, you are writing the password without spaces, right? And with your usual gmail username. If you know anything about python I wrote a [script](https://stackoverflow.com/a/72553333/8838721) very similar to this one (same problem with google) but with smtplib. Maybe if you try it that will help you decide if it's something in your account or it's a c# thing. I really hope you can fix it – Jaime Menendez Jun 10 '22 at 08:13
  • I am able to reproduce [exactly the error](https://ibb.co/RbMsKtx) you describe only when I put the wrong username or password. – Jaime Menendez Jun 10 '22 at 08:20
  • I'm focusing on the connection error, the second account was just for test. I copy the password without the spaces, but with the first account it doesn't get to authenticating. I'm not familiar with python but I've tried your script: `session.login(sender_address, sender_pass) SMTPServerDisconnected: Connection unexpectedly closed` `session.sendmail(sender_address, receiver_address, text) smtplib.SMTPServerDisconnected: please run connect() first`; `session.connect() ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it` – Maria Jun 10 '22 at 10:30
  • I also tried this with firewall turned off, same result – Maria Jun 10 '22 at 11:21
  • You may also want check your email and confirm you added app password.After confirming i was able to send email. – bita pinches Jun 11 '22 at 07:59
  • OMA! I hate this change, why does Google have to *enforce* security? this just makes things more difficult for developers. –  Sep 28 '22 at 09:14
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

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
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.

kevinvi8
  • 191
  • 1
  • 10