Jaeger
Jaeger

Reputation: 1754

Net::SMTPAuthenticationError, 535-5.7.8 Username and Password not accepted

I've viewed previous answers to this problem, but I still can't figure what's wrong with my parameters.

I've modified my gmail account so it allows less secure apps, and unlocked it with a captcha, but it still does not accept my username and password even though I'm sure that they are correct.

Here are my files :

config/environments/production.rb

config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  config.action_mailer.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :user_name            => "[email protected]",
      :password             => "mypassword",
      :authentication       => :login,
      :enable_starttls_auto => true
  }

config/initializers/smtp_settings.rb

ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "mywebsite.fr",
    :user_name => "[email protected]",
    :password => 'myPassword',
    :authentication => :login,
    :enable_starttls_auto => true
}

I guess I've done something wrong, or put something somewhere it doesn't belong. After seeing so many different ways to do it, I'm lost.

Upvotes: 2

Views: 2909

Answers (3)

Kelton Temby
Kelton Temby

Reputation: 875

I came across this issue searching for a fix for Rails 6 since the ENV['GMAIL_USERNAME'] approach doesn't work, and I didn't want my Gmail credentials stored in source (like in first answer on this question).

What did work was putting the credentials in my config/secrets.yml file (not checked into source)

production:
  secret_key_base: xxxxx
  gmail_username: [email protected]
  gmail_password: password

Then in config/environments/production.rb I have the following blocks

 # Use SMTP for emailing for now
  config.action_mailer.delivery_method = :smtp

 # SMTP settings for gmail
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'yourdomain.com',
    user_name:            Rails.application.secrets.gmail_username,
    password:             Rails.application.secrets.gmail_password,
    authentication:       'plain',
    enable_starttls_auto: true
  } 

References: My answer is based on this great answer https://stackoverflow.com/a/24826518/1931185

Upvotes: 0

Abhishek bansal
Abhishek bansal

Reputation: 11

"i have tried everything on stackoverflow but didnot work so i found this solution its the correct way i hope it helps " To Solve smtplib.SMTPAuthenticationError: Username and Password not accepted Error You need to do Is Just Create App Password and then Use it and you'll be able to use SMTP. Just follow the below step to Create App Password. First of all Login into Your Gmail Account. And then Go To MyAccount Section By visiting https://myaccount.google.com Then Open Security Tab in the Sidebar As Shown in the Image. Then You can see There is Signing in to Google section and Make Sure you have turn on two steps verification if Not Then Turn On two steps verification. When You Turn On Your 2-Step Verification then you'll be able to see App Passwords option. And Now Click on App Passwords. Then select app as Mail and select your corresponding device(if you are on window select window mail). Then Click on Generate to create App Password. copy the password. replace your gmail password with this app password in your smtp Now your app Password is been created. Now Use this password in Your SMTP. Just Use this Password in SMTP And Now, Your error must be solved. Thank You.

Upvotes: 1

Sunny Goswami
Sunny Goswami

Reputation: 51

I am a beginner and this is the first time I am posting on stackoverflow and I hope this helps you out. Please let me know if I can help you further! Also if you want to use sendgrid and heroku I can help with that also.

So when it comes to my gmail authentication settings I also had to allow less secure apps, and unlock the captcha. Along with these two changes in gmail settings, I also had to go to my gmail settings>Forwarding and POP/IMAp> then click ENABLE IMAP. Save your changes. These are all the changes I mad to my gmail.

Make the following changes:

config/initializers/setup_mail.rb change authentication to :plain

if Rails.env.development? || Rails.env.production?
        ActionMailer::Base.delivery_method = :smtp
        ActionMailer::Base.smtp_settings = {
            address:        'smtp.gmail.com',
            port:           '587',
            authentication: :plain,
            user_name:      '[email protected]',
            password:       'yourSecureGmailPassword',
            domain:         'mail.google.com',
            enable_starttls_auto: true
        }
     end

config/environments/test.rb host is a website because i am using cloud9

config.action_mailer.default_url_options = { host: 'https://blocipedia-sunnygoo.c9users.io', port: 3000 }

config/environments/development.rb

config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: 'same as in test.rb', port: 3000 }

app/models/user.rb

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable

app/mailers/application_mailer.rb

class ApplicationMailer < ActionMailer::Base
              default from: "[email protected]"
              layout 'mailer'
  end

app/mailers/user_mailer.rb

class UserMailer < ApplicationMailer
               default from: "sunnydgoswami@@gmail.com"

               def new_user(user)
               @user = user
               mail(to: user.email, subject: "Welcome to Blocipedia!")
            end
         end

app/views/layouts/mailer.html.erb

          <html>
               <body>
                    <%= yield %>
               </body>
          </html>

app/views/layouts/mailer.text.erb

<%= yield %>

app/views/user_mailer/new_user.html.erb

'<!DOCTYPE html>
            <html>
                <head>
                     <meta content="text/html; charset=UTF-8" http-
                      equiv="Content-Type" />
                </head>
                <body>
                     <h1>Welcome, new user!</h1>
                     <p>
                         <%= @user.email %>, join us in creating a vibrant wiki 
                             atmosphere!
                     </p>
                     <p>
                          You signed up using <%= @user.email %>. You will be 
                          using this email next time to log in.
                     </p>
               </body>
             </html>'

Upvotes: 5

Related Questions