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

Time:06-09

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 = "[email protected]";
            mail.From = new MailAddress(sender);
            mail.To.Add("[email protected]");
       ;
            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("[email protected]", "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?

CodePudding user response:

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

CodePudding user response:

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