smtp error: 535 5.7.8 Username and Password not accepted for gmail in go
Asked Answered
P

9

18

I'm trying to test an app's outgoing emails from localhost using this function:

func SendContactUsForm(subject, email, body string) error {
    var err error
    from := "[email protected]"
    pass := "somecrazypw"
    to := "[email protected]"
    msg := "From: " + from + "\n" +
    "To: " + to + "\n" +
    "Subject: Contact form:" + subject + "\n" + body
    err = smtp.SendMail("smtp.gmail.com:587",
        smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
        from, []string{to}, []byte(msg))
    if err != nil {
        log.Printf("smtp error: %s", err)
        return err
    }
    return nil
}

But I get this error:

send_emails.go:171: smtp error: 535 5.7.8 Username and Password not accepted. Learn more at 5.7.8 https://support.google.com/mail/?p=BadCredentials a7sm5381413wmh.14 - gsmtp contact.go:38: error seding contact us form 535 5.7.8 Username and Password not accepted.

Despite the fact that the credentials of [email protected] are correct and I have enabled Allow less secure apps on [email protected].

So what could be wrong here? And how can I fix it?

Palmapalmaceous answered 10/6, 2020 at 11:6 Comment(6)
You reposted the same question and deleted the older one. So why post the same question, again? In fact, it was answered as well.Autograft
use Xoauth2 developers.google.com/gmail/imap/xoauth2-protocolFarica
@DaImTo do you imply that it is not possible anymore to use user/password in order access to gmail from apps?Palmapalmaceous
Username and Password not accepted. <-- does imply to me that Google is no longer accepting login and password on their smtp server. your probably the tenth person to ask this in a different language in the past month. Also if the user has 2fa enabled it wont work either.Farica
If its a brand new gmail account that you just created it might not work immediately. This is what happened to me and instead I set up temporarily my normal gmail account and it worked : )Ionium
Double check that Allow less secure apps is enabled. Google passive-aggressively auto-disables it because they hate you and want you to use their crappy app-password feature (even if it's not available for your account).Omdurman
A
20

Enable the 2FA (2 Step Verification) authentication: myaccount.google.com/security if not already

Generate a password from https://security.google.com/settings/security/apppasswords and use that password instead.

An App Password is a 16-digit passcode that gives an app or device restricted access to your Google Account without having to divulge your personal password and complete access to your Google Account.

More details on how to generate one!

Autograft answered 10/6, 2020 at 11:46 Comment(5)
It says: The setting you are looking for is not available for your account.Palmapalmaceous
Enable the 2FA (2 Step Verification) authentication: myaccount.google.com/securityAutograft
I enabled 2FA. Generated 16 character password and used it instead of my original somecrazypw. However I still get the same error. Any ideas?Palmapalmaceous
accounts.google.com/DisplayUnlockCaptcha (Enable it to let your app access your account)Autograft
Just realized that from email should be the same as [email protected] user/passwd, otherwise google does not deliver it and gives error. So [email protected] is not allowed. Thanks for your tips.Palmapalmaceous
H
11

2023: Allowing less secure apps is not permitted. Solution: Login to your gmail account, go to Account Settings> Privacy and enable 2FA. After this search for App Passwords and create one, copy this and enter it as the password for your app. This shall solve any authentication errors, you are facing.

Hydrotherapy answered 10/4, 2023 at 15:45 Comment(1)
+Rep for the year inclusion in answer. Worked well. Thank you!Equilibrist
A
8

If you have enabled 2-factor authentication on your Google account then you can't use your regular password to access mail through code. You need to generate an app-specific password and use that instead your actual password.

Do the following steps to generate app specific password,

  • Log in to your Google account or use this link https://security.google.com/settings/security/apppasswords
  • Go to My Account > Sign-in & Security > App Passwords
  • Scroll down to Select App in the Password & sign-in methds and choose Other (custom name) Give this app password a name, Eg: "automailer"
  • Choose Generate Copy the long generated password and paste it into your script instead of your actual Gmail password.

enter image description here

Appendicular answered 21/12, 2021 at 12:25 Comment(0)
O
5

Go to Security inside your gmail account and enable 3party APP access... This will fix the problem. FYI, I believe this wont work if you have 2-step auth turned on. So you may have to create a new gmail account and then enable forwarding....

Overplay answered 5/1, 2021 at 18:42 Comment(0)
S
1

login to gmail account. go to Manage your google account. turn ON Less secure app access after this when you try to send mail from your app you might get error . if so go to Security issues found ( it's first option in security tab of google account ) here you need to verify that last activities is verified and its you .

Schwejda answered 10/2, 2021 at 8:50 Comment(1)
This setting is no longer available.Interstitial
C
1

1-turn on 2-step verification in your google acount => security

2-in the {Signing in to Google } then go to App passwords and set a password for your app(for example for PHPMailer).then put this password in your sendmail file in your project.

Chloras answered 30/5, 2023 at 19:6 Comment(1)
what should i do if i want to avoid to turning on the 2-step verificationExpectorant
T
0

I was getting user error smtp error: 535 5.7.8, not letting me verify the email. It took me hours trying all the steps above to no avail. Finally, I could add another Gmail address to my main Gmail account.

Open Gmail - click on the setting icon wheel upper right corner Click See All Settings Click Account Under Send Email click Add Another Email Address Enter the name you want to be shown when someone opens the email Enter the email address you are sending from or an alias address ...Next step is where the verification and error message happens Instead of adding your new or alias address and password, add your main G-Mail account username and password.

Good Luck!

Tillford answered 29/7, 2023 at 22:57 Comment(0)
S
0

Just Enable the 2 step verification on then and then only you are able to generate "App Password" for any Gmail account.

Starlight answered 6/3 at 11:53 Comment(0)
A
0
func SendContactUsForm(subject, email, body string) error {
    var err error
    from := "[email protected]"
    pass := "somecrazypw"
    to := "[email protected]"
    msg := "From: " + from + "\n" +
    "To: " + to + "\n" +
    "Subject: Contact form:" + subject + "\n" + body
    err = smtp.SendMail("smtp.gmail.com:587",
        smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
        from, []string{to}, []byte(msg))
    if err != nil {
        log.Printf("smtp error: %s", err)
        return err
    }
    return nil
Assemblage answered 12/4 at 5:16 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has a few answers—including one that has been reasonably well validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Melidamelilot

© 2022 - 2024 — McMap. All rights reserved.