535-5.7.8 Username and Password not accepted

Ruby on-Rails

Ruby on-Rails Problem Overview


I have a contact form and after submitting I am getting a Net::SMTPAuthenticationError 535-5.7.8 Username and Password not accepted

It's pointing to the create action in the contacts controller ContactMailer.new_contact(@contact).deliver

I have restarted the server. I tried https://accounts.google.com/DisplayUnlockCaptcha.

I am in development.

Contacts controller:

 def new
      @contact = Contact.new
    end

    def create
      @contact = Contact.new(params[:message])
      if @contact.valid?
        ContactMailer.new_contact(@contact).deliver
        flash[:notice] = "Message sent! Thank you for contacting us."
        redirect_to root_url
      else
        render :action => 'new'
      end
    end
  end

Development.rb:

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'gmail.com',
    user_name:            '[email protected]',
    password:             'password',
    authentication:       'plain',
    enable_starttls_auto: true  }

  config.action_mailer.default_url_options = { :host => "localhost:3000" }

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

I had the same problem. Now its working fine after doing below changes.

https://www.google.com/settings/security/lesssecureapps

You should change the "Access for less secure apps" to Enabled (it was enabled, I changed to disabled and than back to enabled). After a while I could send email.

Solution 2 - Ruby on-Rails

First, You need to use a valid Gmail account with your credentials.

Second, In my app I don't use TLS auto, try without this line:

config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'gmail.com',
  user_name:            '[email protected]',
  password:             'YOUR_PASSWORD',
  authentication:       'plain'
  # enable_starttls_auto: true
  # ^ ^ remove this option ^ ^
}

UPDATE: (See answer below for details) now you need to enable "less secure apps" on your Google Account

https://myaccount.google.com/lesssecureapps?pli=1

Solution 3 - Ruby on-Rails

Time flies, the way I do without enabling less secured app is making a password for specific app

Step one: enable 2FA

Step two: create an app-specific password

After this, put the sixteen digits password to the settings and reload the app, enjoy!

  config.action_mailer.smtp_settings = {
    ...
    password: 'HERE', # <---
    authentication: 'plain',
    enable_starttls_auto: true
  }

Solution 4 - Ruby on-Rails

UPDATE: > Notice: This setting is not available for accounts with 2-Step Verification enabled, which mean you have to disable 2 factor > authentication.

enter image description here

If you disable the 2-Step Verification:

enter image description here

Solution 5 - Ruby on-Rails

Goto config/initializers/setup_mail.rb Check whether the configuration there matches the configuration written in the development.rb file.It should look like the following in both files:

config.action_mailer.smtp_settings = {
     :address =>"[email protected]",
     :port => 587,
     :domain => "gmail.com",
     :user_name => "[email protected]",
     :password => "********",
     :authentication => 'plain',
     :enable_starttls_auto => true,
     :openssl_verify_mode => 'none' 
     } 

This will most certainly solve your problem.

Solution 6 - Ruby on-Rails

In my case removing 2 factor authentication solves my problem.

Solution 7 - Ruby on-Rails

If you've tried all the answers above, and none of them worked, here is the actual problem. In my case it worked

  1. Gmail doesn't like passwords that are more than 14 symbols. It may work when you login with browser, but from an pplication, it doesn't.

  2. So you have 14 symbol password and you want to change it. Your changes are successful and you can login with the changed password from browser, but guess what, the changes are valid only after few hours. So try to run your applications a few hours later with the changed password, and it will work.

  3. If you want to test that this solution will work for you, just try to fill your personal gmail login and password to see if it works on other accounts.

Solution 8 - Ruby on-Rails

I did everything from visiting http://www.google.com/accounts/DisplayUnlockCaptcha to setting up 2-fa and creating an application password. The only thing that worked was logging into http://mail.google.com and sending an email from the server itself.

Solution 9 - Ruby on-Rails

If you still cannot solve the problem after you turn on the less secure apps. The other possible reason which might cause this error is you are not using gmail account.

-    : user_name  =>  '[email protected]' ,  # It can not be used since it is not a gmail address 
+    : user_name  =>  '[email protected]' ,  # since it's a gmail address

Refer to here.

Also, bear in mind that it might take some times to enable the less secure apps. I have to do it several times (before it works, every time I access the link it will shows that it is off) and wait for a while until it really work.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionxps15zView Question on Stackoverflow
Solution 1 - Ruby on-RailsRavindra BhalothiaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsMrYoshijiView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDat Le TienView Answer on Stackoverflow
Solution 4 - Ruby on-RailsHusam EbishView Answer on Stackoverflow
Solution 5 - Ruby on-Railsuser5287360View Answer on Stackoverflow
Solution 6 - Ruby on-RailsBMA88View Answer on Stackoverflow
Solution 7 - Ruby on-RailsLily HovhannView Answer on Stackoverflow
Solution 8 - Ruby on-RailskryoView Answer on Stackoverflow
Solution 9 - Ruby on-RailsSam Kah ChiinView Answer on Stackoverflow