3
votes

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?

3
You reposted the same question and deleted the older one. So why post the same question, again? In fact, it was answered as well. - xdsarkar
@shmsr you cant delete a question that has an answer on it. - DaImTo
@DaImTo do you imply that it is not possible anymore to use user/password in order access to gmail from apps? - Milkyway
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. - DaImTo

3 Answers

1
votes

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!

3
votes

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....

0
votes

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 .