SMTP Error: 535 5.7.8 Username and Password Not Accepted for Gmail in Go
The SMTP error “535 5.7.8 Username and Password not accepted” typically occurs when there’s an issue with the authentication credentials (username and password) provided for sending emails via Gmail’s SMTP server.
To resolve this error in a Go application, ensure that you’re using the correct Gmail SMTP settings and that the username and password are entered correctly.
Here’s a basic example of how to send an email using Gmail’s SMTP server in Go:
package main
import (
"log"
"net/smtp"
)
func main() {
// Set up authentication credentials
from := "your-email@gmail.com"
password := "your-password"
// Set up SMTP server address and port
smtpHost := "smtp.gmail.com"
smtpPort := "587"
// Set up message
to := []string{"recipient@example.com"}
subject := "Subject of the email"
body := "Body of the email"
// Compose email message
message := "From: " + from + "\n" +
"To: " + to[0] + "\n" +
"Subject: " + subject + "\n\n" +
body
// Authenticate and send email
auth := smtp.PlainAuth("", from, password, smtpHost)
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, []byte(message))
if err != nil {
log.Fatal("Error sending email:", err)
}
log.Println("Email sent successfully")
}
Ensure that you replace "your-email@gmail.com"
and "your-password"
with your actual Gmail email address and password. Also, replace "recipient@example.com"
with the email address of the recipient.
Additionally, make sure that your Gmail account allows access for less secure apps. You can enable this option in your Gmail account settings under “Security” > “Less secure app access”.
If you have two-factor authentication enabled for your Gmail account, you may need to generate an app password specifically for your Go application and use that instead of your regular Gmail password.
By ensuring that you have the correct SMTP settings and authentication credentials, you should be able to resolve the “535 5.7.8 Username and Password not accepted” error when sending emails via Gmail in your Go application.