5

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"}

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");
            }

        }

enter image description here

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

Csibi Norbert
  • 780
  • 1
  • 11
  • 35

3 Answers3

11

What you need to do is to sign-in to your Google Mail account in a web browser, go to Settings, and then check "Enable less secure apps". I believe this url will get you there if you are already signed in: https://myaccount.google.com/lesssecureapps

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • I'm having the same problem over here and already allowed less secure apps but to no use. Besides that, do you have any other suggestions? – Leonardo Dias Jan 30 '20 at 13:12
  • UPDATE (copied from Gmail docs): "To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password." – drizin Jan 08 '23 at 04:45
8

First:

Then:

  • generate a 16-symbols password by going to Gmail Security Settings
  • use the generated code instead of your password for authentication.
Popmedic
  • 1,791
  • 1
  • 16
  • 21
Коля
  • 81
  • 1
  • 1
0

Anyone still having this issue can follow this beautiful link with picturesque instructions:

https://help.warmupinbox.com/en/articles/4934806-configure-for-google-workplace-with-two-factor-authentication-2fa

So you basically need to enable iMAP from Settings (click Gears and not Security Tab)

Then Goto Security Tab -> App Password -> Other(custom name), give it any name and use the generated password.

I haven't tried without disabling two factor authentication though my hunch is it should work without enabling 2FA

N_E
  • 743
  • 10
  • 16