Mailkit does not authenticate with credentials
Asked Answered
B

3

6

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, "[email protected]"));
        // This needs to be put in a configuration file
        message.To.Add(new MailboxAddress("test", "[email protected]"));

        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("[email protected]", "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?

Brenda answered 16/12, 2019 at 19:7 Comment(0)
L
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

Lsd answered 16/12, 2019 at 22:12 Comment(2)
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?Equipoise
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."Survival
M
10

First:

Then:

  • generate a 16-symbols password by going to Gmail Security Settings
  • use the generated code instead of your password for authentication.
Milissa answered 18/7, 2020 at 6:22 Comment(2)
This is the most correct answer I've found.Telemechanics
The accepted answer should be changed to this answer as Gmail stopped supporting the less secure apps option.Caracara
P
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

Perspiratory answered 30/9, 2022 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.