Mailkit does not authenticate with credentials

Csi*_*ert 4 email smtp smtpclient smtp-auth mailkit

I'm trying to send an email with Gmail account. I was able to send emails with yahoo, however, it doesn't work anymore, for some unknown reason. Which i posted a question about that as well, but not response.

In this instance, i'm trying to connect to a gmail account so that I can send emails, but with no luck, it fails after the Connect.

It connects successfully but doesn't authenticate at all, although I have the right credentials.

This is the error i'm getting. I want to mention that the credentials are right.

{"535: 5.7.8 Username and Password not accepted. Learn more at\n5.7.8  
  https://support.google.com/mail/?p=BadCredentials l17sm22879081wro.77 - gsmtp"}
Run Code Online (Sandbox Code Playgroud)

Should I use any certificates or something? I'm sure I'm doing something wrong with the code.

The code

var message = new MimeMessage();

        message.From.Add(new MailboxAddress(contactModel.Name, "s@gmail.com"));
        // This needs to be put in a configuration file
        message.To.Add(new MailboxAddress("test", "a@gmail.com"));

        message.Subject = $"{contactModel.Name} contacted me!";
        message.Body = new TextPart("plain") {
            Text = contactModel.Message + 
            " Details of sender: " + contactModel.EmailAddress + ", " + contactModel.ContactNumber + " ," + contactModel.Name
        };

        using (var client = new SmtpClient())
        {
            try
            {
                if (!client.IsConnected)
                {
                    await client.ConnectAsync("smtp.gmail.com", 587,false);
                    client.AuthenticationMechanisms.Remove("XOAUTH2");
                }
                if (!client.IsAuthenticated)
                {
                    await client.AuthenticateAsync("s@gmail.com", "password");
                }

                await client.SendAsync(message);
                await client.DisconnectAsync(true);

                return "success";
            }
            catch (SmtpCommandException ex)
            {
                throw;
            }
            catch (SmtpProtocolException ex)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new Exception($"The email from {contactModel.EmailAddress} captured but not sent to the owner");
            }

        }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

And I got this email in my inbox, how I can bypass this?

jst*_*ast 7

您需要做的是在网络浏览器中登录您的 Google Mail 帐户,转到“设置”,然后选中“启用安全性较低的应用程序”。如果您已经登录,我相信这个网址会让您到达那里:https : //myaccount.google.com/lesssecureapps