2026-07-15

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

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

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

SMTP Error: 535 5.7.8 Username and Password Not Accepted for Gmail in Go

As developers building applications that rely on external services, dealing with authentication errors is a common hurdle. When setting up outgoing email functionality via an SMTP server, especially with services like Gmail, users often encounter cryptic errors. The specific error you are seeing—smtp error: 535 5.7.8 Username and Password not accepted—is a classic indicator that the credentials being presented to the mail server are failing authentication checks, even if you believe the passwords themselves are correct.

This post will dive deep into why this happens specifically with Gmail SMTP and provide the definitive solutions for getting your Go application sending emails successfully.

Understanding the Authentication Failure

Your Go code snippet demonstrates a standard approach to using the smtp package:

err = smtp.SendMail("smtp.gmail.com:587",
    smtp.PlainAuth("", from, pass, "smtp.gmail.com"), // <-- Authentication happens here
    from, []string{to}, []byte(msg))

The error 535 5.7.8 Username and Password not accepted means that while the connection to the server (smtp.gmail.com:587) was established, the credentials provided for login were rejected by Google's servers. This is rarely an issue with the syntax of your Go code itself; it almost always points to a problem with how the credentials are being validated by the email provider.

The most common reasons this specific error occurs when using Gmail are:

  1. Two-Factor Authentication (2FA): If you have 2FA enabled on your Google account (which is highly recommended for security), simply using your standard account password might be insufficient or blocked by external applications.
  2. Security Deprecation: Google has been tightening security protocols, phasing out older methods like "Less Secure Apps."
  3. App Passwords Requirement: For enhanced security, Google now mandates the use of specific "App Passwords" for third-party application access instead of your main account password.

The Solution: Implementing App Passwords

If you have 2FA enabled on your Gmail account (which is standard), relying on your regular login password will no longer work for SMTP authentication unless you explicitly generate an App Password. This is the most reliable fix.

Step-by-Step Fix for Gmail SMTP

  1. Enable 2FA: Ensure Two-Factor Authentication is active on the Google account you are using as the sender (mysender@gmail.com).
  2. Generate an App Password: Log into your Google Account Security settings. Navigate to the App Passwords section (this feature may be hidden or require specific steps depending on your account type). Generate a unique 16-character password specifically for this application.
  3. Replace the Password in Code: Use this newly generated, unique App Password in place of your regular account password (pass) within your Go function.

Revised Go Example:

func SendContactUsForm(subject, email, body string) error {
    var err error
    from := "mysender@gmail.com"
    // IMPORTANT: Use the App Password generated by Google, not your main account password.
    pass := "your_generated_app_password" 
    to := "mydestination@gmail.com"
    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
}

Beyond Credentials: Modern Email Sending Practices

While App Passwords solve the immediate authentication problem, it is crucial to understand that relying solely on SMTP for high-volume or enterprise applications can introduce maintenance headaches. For robust systems, developers should consider more modern methods.

For complex backend architecture, like those often built using frameworks such as Laravel (which emphasizes secure and scalable service design), integrating dedicated Email Service Providers (ESPs) is often superior. Services like SendGrid, Mailgun, or AWS SES handle the entire delivery infrastructure, security, bounce management, and deliverability issues far better than direct SMTP connections to consumer services like Gmail.

If you are building a large-scale system, moving away from direct credential handling and toward API-based services provides much greater control and reliability. Always prioritize secure, managed solutions when scaling your application's communication layer.

Conclusion

The 535 5.7.8 Username and Password not accepted error in Go when connecting to Gmail SMTP is almost always an authentication failure related to Google’s enhanced security measures, particularly Two-Factor Authentication. The solution lies in generating and using a specific App Password instead of your standard account password. By adopting this practice and considering external ESPs for long-term stability, you ensure your application communicates securely and reliably, paving the way for robust development practices.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.